0

trying to remove multiple object from array,solution i did is fine and working but what i want is i don't want to filter twice, want in a single way

so could you help me for best solution

Example

 const arrList = [{v:'1',l:'label1'},{v:'2',l:'label2'}, 
                  {v:'3',l:'label3'}, {v:'4',l:'label4'}, 
                  {v:'5',l:'label5'}]
const filter1 = arrList.filter((a) => a.l !== 'label1')
const filter3 = filter1.filter((a) => a.l !== 'label3')
console.log(filter3);

1
  • arrList.filter((a) => a.l !== 'label1' && a.l !== 'label3') Commented Sep 18, 2019 at 13:08

6 Answers 6

1

Or you can use a much simpler solution:

arrList.filter((a) => !['label1', 'label3'].includes(a.l))
Sign up to request clarification or add additional context in comments.

1 Comment

1

You are using 2 statement to filter, first not equal to label1 and another statement label3. You can combine them in a single statement with AND && operator, as follow

const filter1 = arrList.filter((a) => a.l !== 'label1' && a.l !== 'label3')

2 Comments

please avoid just pasting code without any explanation
@user2682863 just added the explanation.
0

You can use && to combine logical statements in to a single filter() expression:

const arrList = [{v:'1',l:'label1'},{v:'2',l:'label2'},{v:'3',l:'label3'}, {v:'4',l:'label4'},{v:'5',l:'label5'}]
const filtered = arrList.filter(a => a.l !== 'label1' && a.l !== 'label3')
console.log(filtered);

Comments

0

Try something like this!

 const arrList = [{v:'1',l:'label1'},{v:'2',l:'label2'}, 
                  {v:'3',l:'label3'}, {v:'4',l:'label4'}, 
                  {v:'5',l:'label5'}]
const filter1 = arrList.filter((a) => a.l !== 'label1' && a.l !== 'label3')
console.log(filter1);

Comments

0

const filter3 = arrList.filter((a) => a.l !== 'label1' && a.l !== 'label3')

Comments

0

You could filter it like this.

const myFilter = arrList.filter(label => label.l !== 'label1' && label.l !== 'label3');

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.