2

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.

2 Answers 2

2

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/

Sign up to request clarification or add additional context in comments.

Comments

1

You want to use includes when dealing with filteredNumbersArray.

const remaining = phones.filter(data =>
  !filteredNumbersArray.includes(data.number)
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.