I'm dealing with 'arrays of arrays' and trying to test if all 'sets' contained in the second array are present in the first array.
var arr = [['Netherlands','PP3a'],['Austria','PP16a'],['Estonia','PP3a'],['Luxembourg','PP3a'],['Belgium','PP3a']];
var n = [['Luxembourg','PP3a'],['Netherlands','PP3a'],['Belgium','PP3a']];
In my example https://jsfiddle.net/mnb8jddw/ they clearly are present, but the code (which incidentally seems to work with numbers), reads false. I've obviously got myself confused and would really appreciate some help as I suspect I'm taking the wrong approach.
var arr = [
['Netherlands', 'PP3a'],
['Austria', 'PP16a'],
['Estonia', 'PP3a'],
['Luxembourg', 'PP3a'],
['Belgium', 'PP3a']
];
var n = [
['Luxembourg', 'PP3a'],
['Netherlands', 'PP3a'],
['Belgium', 'PP3a']
];
function searchForArray(haystack, needle) {
var i, j, current;
for (var i in haystack) {
if (needle.length === haystack[i].length) {
current = haystack[i];
for (j = 0; j < needle.length && needle[j] === current[j]; ++j);
if (j === needle.length)
return i;
}
}
return -1;
}
console.log(searchForArray(arr, n)); // -1 = false
searchForArray(arr,n[0]);would returns 3.