Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda