1

I'm creating this function that returns only those objects (from an object array) whose property "a" value is not in another array of excluded numbers (which are actually strings)

I realized that I need a double iteration, tried to nest a filter, inside a filter but didn't succeed. I had to hardcode it using the OR operator like this:

const arr = [{a:"1"},{a:"2"},{a:"3"}]

const filtered = (arr)=>{
  const ex = ["1","2"]

  return arr.filter(e =>   !e.a.includes(ex[0] || ex[1]))
  
}
console.log(filtered(arr));

How can I loop this properly so that I don't have to use the || operator?

1
  • That's not how you use the || operator. If you need two test in two variables, it should be !e.a.includes(ex[0]) || !e.a.includes(ex[0]). Also, || should be &&. Commented Jul 20, 2021 at 15:26

2 Answers 2

6

You don't need two loops. You have the includes backwards.

return arr.filter(e => !ex.includes(e.a))
Sign up to request clarification or add additional context in comments.

2 Comments

1. filter 2. includes - looks like 2 loops to me)
The OP had multiple calls to include, so he was talking about another loop.
4

const arr = [{a:"1"},{a:"2"},{a:"3"}]

const filtered = (arr)=>{
  const ex = ["1","2"]

  return arr.filter(e => !ex.includes(e.a))
  
}
console.log(filtered(arr));

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.