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

143
Views
Filter Array with objects based on another array dynamically

Filter Array with objects based on another array dynamically.

I need to filter a main array using another array. But the filter array will only contain a few fields or several.

    var codes = [{
"title": "lore",
"city": "New York",
"desc": "lorem lorem",
"age": "32"
 },
{
    "title": "lore2 ",
    "city": "Santa Monica",
    "desc": "lorem2",
    "age": "20"
  }

];

let filter = [{
  "city": "New York"
}, {
  "age": "20"
},

...Or more filters. This filter is dynamically created by the person clicking on the checkbox.

]

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This should work

let codes = [{
    "title": "lore",
    "city": "New York",
    "desc": "lorem lorem",
    "age": "20"
  },
  {
    "title": "lore2 ",
    "city": "Santa Monica",
    "desc": "lorem2",
    "age": "20"
  }
];

let filter = [{
  "city": "New York"
}, {
  "age": "20"
}];

let res = codes.filter(code => {
  return filter.every(f => {
    const [key, value] = Object.entries(f)[0];
    return code[key] == value
  });
});

console.log(res)

about 4 years ago · Juan Pablo Isaza Report

0

You could take a better data structure which is easier to use instead of unknown keys.

As result you get object which all keys/values match.

const
    codes = [{ title: "lore", city: "New York", desc: "lorem lorem", age: "32" }, { title: "lore2 ", city: "New York", desc: "lorem2", age: "20" }],
    filter = [{ key: "city", value: "New York" }, { key: "age", value: "20" }],
    result = codes.filter(o => filter.every(({ key, value }) => o[key] === value));

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Maybe something like this could work for you?

Basically looping through filters and then also through the seperate filter objects key-value pairs and checking them against the codes.

var codes = [{
  "title": "lore",
  "city": "New York",
  "desc": "lorem lorem",
  "age": "32"
},
{
  "title": "lore2 ",
  "city": "Santa Monica",
  "desc": "lorem2",
  "age": "20"
}];

var filters = [{"city": "New York"}, {"age": "32"}];


function filterByMultipleFilters(data, filters) {
  return data.filter(item => {
    // Loop through all filters and check the current item
    for (const filter of filters) {
      for (const [key, value] of Object.entries(filter)) {
        if (!item[key] || item[key] !== value)
          return false;
      }
    }
    return true;
  });
}

console.clear();
let result = filterByMultipleFilters(codes, filters);
console.log(result);

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!