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

246
Vistas
JS - merge 2 arrays based on properties

I'm trying to merge 2 arrays of objects where the resulting array should have only objects present in array 1 (selectedNames) but with one property from corresponding objects from array 2 (nameDetails). There's one matching (unique) property to match them:

const selectedNames = [
  {
    id: '11'
    name: 'joe',
  },
  {
    id: '22',
    name: 'bill',
  },
];

const nameDetails = [
  {
    nameId: '11',
    salary: '23422',
    location: 'New Jersey',
  },
  {
    nameId: '33',
    salary: '23424',
    location: 'New York',
  },
  {
    nameId: '22',
    salary: '99999',
    location: 'Boston',
  },
  { nameId: '44',
    salary: '323232',
    location: 'Chicago',
  },
];

The matching property is selectedNames.id === nameDetails.nameId. All entries in selectedNames will definitely be present in nameDetails (but not the other way round). The resulting array should look like that:

[
  {
    id: '11',
    name: 'joe',
    salary: '23422',
  },
  {
    id: '22',
    name: 'bill',
    salary: '99999'
  }
]

I'm a bit confused. I know it'll probably consist of .includes() and filter() and ... for merging? I'm not sure how to handle it.

Alternatively, which will probably be much easier, filter the nameDetails array to have only objects with nameId that exists (as id) in selectedNames.

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

0

I am a bit confused by your example result. For example where does id: '11111' come from?

Are you looking for something like this maybe?

const selectedNames = [
  {
    id: '11',
    name: 'joe',
  },
  {
    id: '22',
    name: 'bill',
  },
];

const nameDetails = [
  {
    nameId: '11',
    salary: '23422',
    location: 'New Jersey',
  },
  {
    nameId: '33',
    salary: '23424',
    location: 'New York',
  },
  {
    nameId: '22',
    salary: '99999',
    location: 'Boston',
  },
  { nameId: '44',
    salary: '323232',
    location: 'Chicago',
  },
];

const merged = selectedNames.map(n => {
  n.salary = nameDetails.filter(d => n.id === d.nameId)[0].salary;
  return n;
});

console.log(merged)

Note: This will change the original selectedNames by adding the salary from the corresponding entry in nameDetails.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe this fits your needs:

selectedNames.map(({ id, name }) => {
    let salary = nameDetails.find((nameItem) => nameItem.nameId === id).salary;
    return { id, name, salary };
})

Will result in:

[
  {
    id: '11',
    name: 'joe',
    salary: '23422',
  },
  {
    id: '22',
    name: 'bill',
    salary: '99999',
  }
]
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