Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Best way to filter an array by multiple properties?

I'm trying to filter an array and remove the entries that contain certain properties. For example, my array looks like this:

const items = [{a: "apple", b: "banana", c: "coconut"}, {a: "apple"}, {b: "banana, c: "coconut"}];

And I want to filter out the items that have the properties b or c.

Right now I have:

items.filter(item => "b" in item || "c" in item);

but I feel like there is a better way to do this, and set it up so a lot more properties could easily be added in the future.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Put the property names in an array, and use .some() to test if any of those properties exists.

const items = [{
  a: "apple",
  b: "banana",
  c: "coconut"
}, {
  a: "apple"
}, {
  b: "banana",
  c: "coconut"
}];

let properties_to_filter = ["b", "c"]
let result = items.filter(item => properties_to_filter.some(prop => prop in item));

console.log(result);

about 4 years ago · Santiago Trujillo Report

0

      const items = [{a: "apple", b: "banana", c: "coconut"}, {a: "apple"}, {b: "banana", c: "coconut"}]

      let filtredKey = ["a"]

      let result = []
      items.map(el => {
        if(el.hasOwnProperty("c"))
          result.push(el)
      })

      console.log(result)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!