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

109
Views
Filter an array of objects based on another array

I want to filter this array of Object:

const testArray = [{
  id: 4,
  filters: ["Norway", "Sweden"]
}, {
  id: 2,
  filters :["Norway", "Sweden"]
}, {
  id: 3,
  filters:["Denmark", "Sweden"]
}]

with the filter

const myFilter=["Norway", "Sweden"]

However my code just returns []? What I have tried so far:

const testArray = [{
  id: 4,
  filters: ["Norway", "Sweden"]
}, {
  id: 2,
  filters :["Norway", "Sweden"]
}, {
  id: 3,
  filters:["Denmark", "Sweden"]
}]
const myFilter=["Norway", "Sweden"]

console.log(testArray.filter(e=>e.filters===myFilter))
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use Array#every to test the equality+ of two arrays as follows:

const output = testArray.filter(
    e => e.filters.every(
        filter => myFilter.includes(filter)
    )
);

const 
testArray = [{ id: 4, filters: ["Norway", "Sweden"] }, { id: 2, filters :["Norway", "Sweden"] }, { id: 3, filters:["Denmark", "Sweden"] }],
myFilter = ["Norway", "Sweden"],

output = testArray.filter(
    e => e.filters.every(
       filter => myFilter.includes(filter)
    )
);

console.log(output)

NOTE: Array#every as used here just confirms that every element of each array is in the other, regardless of the order in which the elements appear.

about 4 years ago · Juan Pablo Isaza 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!