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

201
Vistas
Javascript: How to use map and filter between two array object

I am facing some issue on using .map and .filter where I am unable to get the object which is not similar in two objects. What changes I need to do to get the uncommon object from arrayObjTwo.

code

const arrayObjOne = [{
          countryCode: "US",
          description: " Backyard of home",
          id: "1234",
          location: "US",
          name: "Backyard",
          }]
// Array Object 2
const arrayObjTwo =[
    { description: "Backyard of home", spaceName: "Backyard" },
    { description: "Frontyard of home", spaceName: "Frontyard"},
]
const object1Names = arrayObjOne.map(obj => obj.Name); // for caching the result
const results = arrayObjTwo.filter(name => !object1Names.includes(name));
console.log(results);

One compiler code: https://onecompiler.com/javascript/3xy92hpmp

expected result:

const arrayObjTwo =[
        { description: "Frontyard of home", spaceName: "Frontyard" }
    ]

Thanks..

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your filter for the result is wrong. You should be using the "object.spaceName" instead of just "name" as you're looping through an array of objects and not just strings

// So this line:
const results = arrayObjTwo.filter(name => !object1Names.includes(name));

// Should be:
const results = arrayObjTwo.filter(object => !object1Names.includes(object.spaceName));

// And you have a typo:
const object1Names = arrayObjOne.map(obj => obj.Name);

// Should be like so as keys and properties are case-sensitive:
const object1Names = arrayObjOne.map(obj => obj.name);

So this should all be:

const arrayObjOne = [{
          countryCode: "US",
          description: " Backyard of home",
          id: "1234",
          location: "US",
          name: "Backyard",
          }]
// Array Object 2
const arrayObjTwo =[
    { description: "Backyard of home", spaceName: "Backyard" },
    { description: "Frontyard of home", spaceName: "Frontyard"},
]
const object1Names = arrayObjOne.map(obj => obj.name); // for caching the result
const results = arrayObjTwo.filter(object => !object1Names.includes(object.spaceName));
console.log(results);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You're close!

You have capitalized names in your map-statement, but javascript is case-sensitive, so the field you're looking up "Name", does not exist, and as such your "object1Names" returns undefined.

You should be looking up obj.name

You also need to compare it to the arrayObjTwo's "spaceName"-field, not the entire object.

Here's your code with these small changes:

const arrayObjOne = [{
          countryCode: "US",
          description: " Backyard of home",
          id: "1234",
          location: "US",
          name: "Backyard",
          }]
// Array Object 2
const arrayObjTwo =[
    { description: "Backyard of home", spaceName: "Backyard" },
    { description: "Frontyard of home", spaceName: "Frontyard"},
]
const object1Names = arrayObjOne.map(obj => obj.name); // for caching the result
const results = arrayObjTwo.filter(obj => !object1Names.includes(obj.spaceName));
console.log(results);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Do you need this?

const arr1 = [{
          countryCode: "US",
          description: " Backyard of home",
          id: "1234",
          location: "US",
          name: "Backyard",
          }]
// Array Object 2
const arr2 =[
    { description: "Backyard of home", spaceName: "Backyard" },
    { description: "Frontyard of home", spaceName: "Frontyard"},
]
const names = arr1.map(item => item.name); // for caching the result
console.log(names);
const res = arr2.filter(item => !names.includes(item.name));
console.log(res);

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