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

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

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 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