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

How to filter an array using another array in Javascript?

I have an array of objects that has "number" property in it, i want to filter this array based on the "number" property through an array of numbers called "filteredNumbersArray"

i tried this but it doesn’t make any changes to the array

const remaining = phones.filter(
          (data) => !((data.number as number) in filteredNumbersArray),
        );

const remaining is the filtered phones array.

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

phones is my array of object, my object has a field number: number

example on data in phones: [{number: 5}, {number:2}]

filteredNumbersArray is an array of numbers. ex.[1,2,3]

i want result to be [{number:5}] based on the above example.

>Solution :

you could use array.includes to know if it contain an element

const phones = [{number: 5}, {number:2}] ;
const filteredNumbersArray = [2,4,6];
console.log( phones.filter( e => !filteredNumbersArray.includes(e.number) ) );

https://jsfiddle.net/fgsrjnm6/

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