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

96
Vistas
Check if an a propery of an object from an array is contained in another array of object

Let's say I have these arrays of object

selectedNames = [
   {label: 'Nicolas', value:'Nicolas'},
   {label: 'Michael', value:'Michael'},
   {label: 'Sean', value:'Sean'},
   {label: 'George', value:'George'}
]

selectedWhatever = [
   {label: 'Guitar', value: 'Guitar'},
   {label: 'Bass', value: 'Bass'}
]

and then another array of objects like this:

list = [
   {name: 'Nicolas', surname: 'Smith', birthplace: 'NewYork', whatever: 'Guitar'},
   {name: 'Sean', surname: 'Narci', birthplace: 'LA', whatever: 'Bass'},
   {name: 'George', surname: 'Rossi', birthplace: 'NewYork', whatever: 'Bass'},
   {name: 'Fiona', surname: 'Gallagher', birthplace: 'Madrid', whatever: 'Drum'},
   {name: 'Michael', surname: 'Red', birthplace: 'London', whatever: 'Triangle'},
]

and I want to filter list based on the datas I have in the others two arrays and group by birthplace, so I want this:

result = {
   LA: [
      {name: 'Sean', surname: 'Narci', birthplace: 'LA', whatever: 'Bass'},
   ],
   NewYork: [
      {name: 'Nicolas', surname: 'Smith', birthplace: 'NewYork', whatever: 'Guitar'},
      {name: 'George', surname: 'Rossi', birthplace: 'NewYork', whatever: 'Bass'},
   ]
}

What I've done is the following and it works fine. Is there a smarter or more elegant way to do the same?

const obj = {};

list.map((res) => {
   if ((data.selectedNames.map((r) => r.label).includes(res.name))
      && (data.selectedWhatever.map((r) => r.label).includes(res.whatever))
   ){
      result[res.birthplace] = result[res.birthplace] ?? []:
      result[res.birthplace].push(res);
   }
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Instead of creating and searching a new array of names and whatever for every checked entry, one could create those only once and store them together with the key to he checked as Sets:

 const filters = {
    name: new Set( selectedNames.map(it => it.label) )
 };

 const result = list.filter(it => 
    Object.entries(filters).some(([key, set]) =>
        set.has( it[key] )
     )
  );

The grouping part should really also be a reusable function.

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