indexOf syntax for multidimensional arrays?

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,615
Reaction score
1,417
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
454
Reaction score
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
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,615
Reaction score
1,417
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

Hi, there.
I'm Creta Park, as Software engineer and Indie game developer.

Profile : creft.me/cretapark
GitHub : github.com/creta5164
Blog : creta5164.tumblr.com


My stuff

Who shares/sells their scripts/plugins on itch.io? Can I pick your brain?
If you ever need fantasy names and you're bored, make a fictional alphabet. Switch around the letters and see what you get :p
I JUST GOT PROMOTED TO 9TH GRADE BOIS!

Forum statistics

Threads
98,170
Messages
949,972
Members
129,461
Latest member
daianeppiresx
Top