Filter Coordinate Array from Another Coordinate Array

    let big_coordinates_arr  =[ [1,2],[3,4],[5,8],[7,9]] ;
    let small_coordinates_arr=[ [3,4],[7,9] ] ;

Exepected Output: [ [1,2],[5,8] ]

Dear all, is there a way to filter an array of coordinates from an array of coordinates? There are codes similar to this from element of array at here, but I am unsure on filtering 2d-nested array.

I will appreciate if anyone can guide me 🙂

>Solution :

Try this one.

let big_coordinates_arr  =[ [1,2],[3,4],[5,8],[7,9]] ;
let small_coordinates_arr=[ [3,4],[7,9] ] ;

function check(data) {
    for (i = 0; i < small_coordinates_arr.length; i++) {
        //checking both 0 and 1 index at the same time and
        //eliminating them immediately.
        if (data[0] === small_coordinates_arr[i][0] && data[1] === small_coordinates_arr[i][1]) {
            return false;
        }
    }
    //if element doesn't match keep them by returning true
    return true;
}

console.log(big_coordinates_arr.filter(check)); //you will get [ [1,2],[5,8] ]

But this solution works if you want to exclude all matched elements. I mean, if big_coordinates_arr has [3,4] element twice or more, this algorithm eliminates all of them.

Leave a Reply