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

182
Visualizações
How to filter an array of objects based on another array of object's property value?

I have two sample arrays below.

let arr1 = [
  { key: "WIHUGDYVWJ", className: "science" },
  { key: "qwljdhkuqwdnqwdk", className: "english" },
  { key: "likubqwd", className: "robotics" }
];
let arr2 = [
  { key: "WIHUGDYVWJ", title: "math" },
  { key: "qwljdhkuqwdnqwdk", title: "english" },
  { key: "likubqwd", title: "robotics" }
];
  1. How can I filter arr1 to get only items that have 'className' value that matches arr2's item's 'title' value? (expecting only items with 'english' and 'robotics' to remain)

  2. How can I filter arr1 to get only items that have 'className' value that do not match arr2's item's 'title' value? (expecting only items with 'science' to remain)

Thanks!

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

0

let arr1 = [
  { key: "WIHUGDYVWJ", className: "science" },
  { key: "qwljdhkuqwdnqwdk", className: "english" },
  { key: "likubqwd", className: "robotics" },
  { key: "abcd", className: "history" }
];

let arr2 = [
  { key: "WIHUGDYVWJ", title: "math" },
  { key: "qwljdhkuqwdnqwdk", title: "english" },
  { key: "likubqwd", title: "robotics" }
];

// q1 answer
console.log(arr1.map(arr1Item => arr2.filter(arr2Item => arr1Item.className === arr2Item.title)).flat());

// q2 answer
console.log(arr1.filter(arr1Item => !arr2.some(arr2Item => arr2Item.title === arr1Item.className)));

i wrote without using a for, if/else statement as much as possible. this code may not be the best. i think it could be more improved.

I hope my answer is helpful

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