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

171
Vistas
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 Respuestas
Responde la pregunta

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