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.
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.
CodePudding user response:
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/
CodePudding user response:
You want to use includes
when dealing with filteredNumbersArray
.
const remaining = phones.filter(data =>
!filteredNumbersArray.includes(data.number)
)