indexOf syntax for multidimensional arrays?

Discussion in 'Learning Javascript' started by Jonforum, Dec 18, 2016.

  1. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,574
    Likes Received:
    1,339
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    hi


    i know i can do the function with (for) and (loop), but logically, am supposed to be able to get the indexOf ?


    Why my function to search index in a 2D array alway return me False (-1) ???


    If you add this to console.log


    //------------------------------------------
    gate1=[];
    gate1=[[3,4],[4,6]]; // test [[ItemID,NBItem],[ItemID,NBItem]]
    //------------------------------------------
    function CheckGiveItem(ID, NEED, GVariable) {
    var found = [ID[0],NEED[0]]; // = [3,4]
    return GVariable.indexOf(found);
    }
    //------------------------------------------
    $dataArmors[1].meta.test = {
    //test indexof 2D array
    check: CheckGiveItem([3,6,10,11],[4,6,8,7],gate1) //example ([ItemID],[NEED],GlobalGameVariable)
    };


    and after this to test


    $dataArmors[1].meta.test.check


    is return me -1


    i do the same thing like i read here.


    http://stackoverflow.com/questions/1427640/indexof-syntax-for-multidimensional-arrays
     
    Last edited by a moderator: Dec 18, 2016
    #1
  2. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    Because essentially what is happening is you are comparing an array to an array which will always be false.


    Example:


    //This is basically what you are telling indexOf to do
    [3, 4] === [3, 4] // -1


    Over at the stack overflow question they weren't wanting to pass an entire array just a single element contained in the inner array. They could have gotten around this very easily by doing:

    Code:
    x = [[a, b]]
    
    x[0].indexOf(a) // returns 0
    x[0].indexOf(b) // returns 1
     
    #2
  3. Jonforum

    Jonforum Veteran Veteran

    Messages:
    1,574
    Likes Received:
    1,339
    Location:
    Canada / Québec
    First Language:
    French
    Primarily Uses:
    RMMV
    @Sarlecc 
    I Have the impression that everything is done to make basic things more complicated sometimes
     


    I abandoned the idea of indexOf for arrays, but i finally found a very better great way alternative. :rock-left:
    Using a hash and return return with ID.map


    So is give this


    //------------------------------------------
    gate1=[];
    gate1=[[3,4],[4,6]]; // test [[ItemID,NBItem],[ItemID,NBItem]]
    //------------------------------------------
    function CheckGiveItem(ID, NEED, GVariable, SingArr) {
    console.log('ID '+ID+' NEED '+NEED);
    if (SingArr) {return 'if need return individual check'}
    var TestHash = Object.create(null);//HaCH
    GVariable.forEach(function (a) {
    TestHash[a.join('|')] = true;
    });
    return ID.map(function (id, i) {
    return TestHash[[id, NEED].join('|')] || false;
    });
    }
    //------------------------------------------
    $dataArmors[1].meta.test = {
    //test return each instance of subquest
    IsFinish: function (SingArr){return CheckGiveItem([3,6,4,11],[4,6,6,7],gate1,SingArr)} //([ItemID],[NEED])
    };


    And , simply need to use function to compare a var X[ [ , ],[ , ],[ , ],[ , ] ] with item array injected


    $dataArmors[1].meta.test.IsFinish() // Will return [true,false,true,false]




    This are very more effective and test each array instance [itemID,Quantity] in the necessary variable holding the dynamic array.
    And the order doesn't matter. :)


    so for example if return me [true,true,true,true] the combinaison for builder the item, or the house, and the quest return complet.


    Also thanks you a lot to trying to help me and your explication about the indexOf
     
    Last edited by a moderator: Dec 18, 2016
    #3

Share This Page