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

65
Visualizações
Filtering Data using Properties values using ES6

Like to filter data based on properties values from Object Let's say I have an object:

{
  A: { value1: 4, value2: 2, value3: 5 },
  B: { value1: 2, value2: 5, value3: 8 },
  ...
}

I want to create object by filtering the object above so I have something like if I filter based on value1: 4 and value2: 2

{
  value1: 4,
  value2: 2,
  value3: 5
}

I am looking for a clean way to accomplish this using Es6.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

const 
  obj = { A: { value1: 4, value2: 2, value3: 5 }, B: { value1: 2, value2: 5, value3: 8 } },
  filter = { value1: 4, value2: 2 };

// get key-value pairs of filter  
const filterPairs = Object.entries(filter);
  
// iterate over obj's values, and return the ones matching filterPairs
const matches = 
  Object.values(obj)
  .filter(o => filterPairs.every(([key, value]) => o[key] === value));
  
console.log(matches);

Note: if you only want to get the first matching object, use Array#find instead of Array#filter

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