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

146
Visualizações
Filtering array inside object

I need to filter an array inside the object and return a new array that matches the filter

const example = [ { id: 1, b: [1, 2]}, {id:2, b:[1]}, {id 3, b: [1,3]}
const filter = 1

the return of the filter should just be [{id: 2, b:[1]}]

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

0

1) If you only want an object that only contain 1 inside it. then you can it using filter do as:

o.b.length === 1 && o.b[0] === filter

const example = [
  { id: 1, b: [1, 2] },
  { id: 2, b: [1] },
  { id: 3, b: [1, 3] },
];
const filter = 1;

const result = example.filter((o) => o.b.length === 1 && o.b[0] === filter);
console.log(result);

2) Using includes and filter

const example = [
  { id: 1, b: [1, 2] },
  { id: 2, b: [1] },
  { id: 3, b: [1, 3] },
];
const filter = 1;

const result = example.filter((o) => o.b.length === 1 && o.b.includes(filter));
console.log(result);

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