oussama kamal oussama kamal - 6 months ago 21
Javascript Question

Javascript mach coordinates between arrays

I have 3 arrays in Javascript :

var arr1 = [[34.086586, -84.52345500000001], [34.080705, -84.52081499999997], [34.136911, -83.97300999999999], [34.090184, -84.51971000000003], [33.99105, -83.717806]];
var arr2 = [[34.29712, -83.86256700000001]];
var arr3 = [[33.99105, -83.717806]];


How can I check if arr2 or arr3 are inside arr1 ?

Thanks

Answer

You could iterate over the haystack and the needles and if the lenght of the arrays inside is equal, check every value.

function check(haystack, needles) {
    return haystack.some(function (h) {
        return needles.some(function (n) {
            return h.length === n.length && h.every(function (a, i) {
                return a === n[i];
            });
        });
    });
}

var arr1 = [[34.086586, -84.52345500000001], [34.080705, -84.52081499999997], [34.136911, -83.97300999999999], [34.090184, -84.51971000000003], [33.99105, -83.717806]],
    arr2 = [[34.29712, -83.86256700000001]],
    arr3 = [[33.99105, -83.717806]];

console.log(check(arr1, arr2));
console.log(check(arr1, arr3));

Comments