Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading