I have an array with nested arrays that looks like this:
var tw = [[3, 0], [11, 0], [3, 14], [11, 14]];
When I try and find if the array tw contains a passed in array, I always get a result of -1.
For example:
var test = $.inArray([3, 0], tw);
var test2 = tw.indexOf([3, 0]);
both return -1, even though the first object in the array is [3,0]
How do I find out if a specific array of arrays is contained in my array?
Oh, and I’ve only tested it on IE9 so far.
That’s because you’re searching for a different object.
indexOf()uses strict equality comparisons (like the===operator) and[3, 0] === [3, 0]returns false.You’ll need to search manually. Here’s an example using a more generic
indexOf()function that uses a custom comparer function (with an improvement suggested by @ajax333221 in the comments):