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

138
Vistas
Filter array of objects with another array of object without knowing which properties are in the objects

I have an array of objects that I would like to filter but I do not know the properties that I have in the objects (neither the data nor the filter array):

myArray = [
   { id: "100", area: "01", country: "AT"},
   { id: "101", area: "02", country: "DE"}, 
   { id: "102", area: "01", country: "DE"},
   { id: "103", area: "03", country: "CH"},
   { id: "104", area: "01", country: "AT"}
]

and my filter is:

myFilter = { area: "01", country: "AT" }

I would like to have this returned:

myArrayFiltered = [
   { id: "100", area: "01", country: "AT"},
   { id: "104", area: "01", country: "AT"}
]

How would I go about doing this? Just to be perfectly clear, I do not know the properties that I will be searching for i.e. I do not know which properties myArray or myFilter will have. If the filter contains a key that is not in myArray I would expect an empty array!

PS: I do also not know how many entries are going to be filters by i.e. depending what my users enter I could just get:

myFilter = [ {area: "01"} ]
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

myArray.filter((item) => 
 Object.entries(myFilter).every(([key, val]) => item[key] === val))
about 4 years ago · Santiago Trujillo Denunciar

0

var myArray  = 
[
    { id: "100", area: "01", country: "AT"},
    { id: "101", area: "02", country: "DE"}, 
    { id: "102", area: "01", country: "DE"},
    { id: "103", area: "03", country: "CH"},
    { id: "104", area: "01", country: "AT"}
 ]

 var  myFilter = { area: "01", country: "AT" }

    var filteredArray = myArray.filter(function(item) {
        return Object.keys(myFilter).every(function(c) {
            return item[c] === myFilter[c];
        });
    } );
    
    console.log(filteredArray)

about 4 years ago · Santiago Trujillo Denunciar

0

myArray.filter((item) => Object.keys(item).every((key) => !myFilter[key]|| myFilter[key] === item[key]))
about 4 years ago · Santiago Trujillo 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