Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda