indexOf syntax for multidimensional arrays?

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
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:

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
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
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
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:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,849
Messages
1,016,977
Members
137,563
Latest member
cexojow
Top