Testing for an array within an array

Status
Not open for further replies.

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
I have an array where each element is an array of an x, y pair:

this.myArray: Array[2] 0: Array[2] 0: 1 1: 13 1: Array[2] 0: 2 1: 13I am trying to see if an x, y pair is contained in the array:

... = function(x, y) { return this.myArray.indexOf([x,y]) !== -1;}but this.myArray.indexOf([x,y]) is always equal to -1.

My JS book says indexOf uses a === equality tester. So I'm guessing it's just something to do with [1,2] === [1,2] not being true?

I will try this out:

Code:
... = function(x, y) {  return this.myArray.some(function(key) { return key[0] === x && key[1] === y }.bind(this));}
and use it if it works, but I'd really like to know why the original doesn't?
 

Sulsay

Villager
Member
Joined
Nov 25, 2015
Messages
25
Reaction score
7
First Language
Dutch
Primarily Uses
RMMV
In javascript,

[1] === [1]will always return false. This is because arrays (and objects) are compared by reference, not by value. Both arrays point to a different location in memory.For scalars, such as integer 1, or string 'someString', the comparison is value-based, and thus

1 === 1will return true.If you were to do something like this, the result would be true because both variables reference the same array:

Code:
var x = [1];var y = x;var equal = x === y; // true
Your best option to check for equality is indeed to check the values in a loop. I think you're looking for every instead of some?
 

Quxios

Veteran
Veteran
Joined
Jan 8, 2014
Messages
1,055
Reaction score
785
First Language
English
Primarily Uses
RMMV
The 1st case would only work if you check it with the same variable.

var test = [1, 2];var test2 = [test];test2.contains(test); // true because we used the object we put in the arraytest2.contains([1, 2]); // false because it creates a new objectI've come across this problems a few times. One way to get around it is to store in a string of the points instead of an array. Another is to store a variable that's unique to every point, I don't recall the name for this but the formulas:

i = x + y * maxwidth;which is what I ended up using.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
Thanks.


Using myArray.some did the trick. I only want to know if ANY array element has the same values, not if every one of them has.
 

Sulsay

Villager
Member
Joined
Nov 25, 2015
Messages
25
Reaction score
7
First Language
Dutch
Primarily Uses
RMMV
Then some() is indeed fine. Maybe you can simplify the check to something like this:

Code:
var array1 = [1, 2, 3],    array2 = [3, 4, 5];	var oneValueMatches = array1.some(function (value) { return array2.contains(value); })
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
I think you are missing what I am trying to do. Array1's elements are arrays themselves, each of length 2. And I want to see if a specific array of length 2 is present in Array1. I am comparing arrays, not individual values.


I've already got it working, so the following is only by way of explanation.


Array1 contains a list of x/y coordinates that meet my conditions.


Array1:


0: [1,1]


1: [1,2]


2: [1,3]


3: [2,2]


I want to see if the coordinate x, y is contained in that list. So if [x,y] is [1,2] it would return true; if [x,y] is [2,1] it would return false.


contains would have worked in Ruby, but its implement in the core scripts use indexOf, which takes me back to where I was.


Anyway, I've got it working, so thanks for the input everyone.
 
Last edited by a moderator:

Sulsay

Villager
Member
Joined
Nov 25, 2015
Messages
25
Reaction score
7
First Language
Dutch
Primarily Uses
RMMV
You're right, I missed that. Glad you got it working! ;-)
 
Status
Not open for further replies.

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

Latest Threads

Latest Posts

Latest Profile Posts

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.
time for a new avatar :)

Forum statistics

Threads
106,017
Messages
1,018,356
Members
137,802
Latest member
rencarbali
Top