Testing for an array within an array

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?
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?
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.
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.
=== doesn't convert the data types to the same type when comparing, == does. 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

This doesn't really answer the question you are seeking, but it does give more insight about the comparison you are doing.
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); })
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.
You're right, I missed that. Glad you got it working! ;-)

Original thread: https://forums.rpgmakerweb.com/threads/testing-for-an-array-within-an-array.52424/