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

265
Vistas
JavaScript lookup: update value of object in array if object key exists in another object array similar to the v-lookup in excel

Just trying to update the dates in array2 if ID matches in array1 so that they are not null.


let array1 = [{"id":1, "date": "23/11/21"}, {"id":2, "date":"20/11/21"}, {"id":3, "date":"15/11/21"}]

let array2 = [{"id":1, "name": "John", "date": null}, {"id":2, "name": "Max", "date": null}, {"id":3, "name": "Peter", "date": null}]

Desired output:

let array2 = [{"id":1, "name": "John", "date":"23/11/21" }, {"id":2, "name": "Max", "date": "20/11/21"}, {"id":3, "name": "Peter", "date": "15/11/21"}]

How do I use a loop with the indexof() method?

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

0

You could use a map method to iterate trough the second array, find an element with the same id in the first array and take the date from there:

let array1 = [{
  "id": 1,
  "date": "23/11/21"
}, {
  "id": 2,
  "date": "20/11/21"
}, {
  "id": 3,
  "date": "22/11/15"
}]

let array2 = [{
  "id": 1,
  "name": "John",
  "date": null
}, {
  "id": 2,
  "name": "Max",
  "date": null
}, {
  "id": 3,
  "name": "Peter",
  "date": null
}];

const updated = array2.map(el => {
  const isIdInFirstArr = array1.find(e => e.id === el.id);
  if (isIdInFirstArr) {
    el.date = isIdInFirstArr.date;
  }
  return el;
})

console.log(updated)

about 4 years ago · Juan Pablo Isaza Denunciar

0

1) You can efficiently achieve this using Map as:

let array1 = [
  { id: 1, date: "23/11/21" },
  { id: 2, date: "20/11/21" },
  { id: 3, date: "15/11/21" },
];
let array2 = [
  { id: 1, name: "John", date: null },
  { id: 2, name: "Max", date: null },
  { id: 3, name: "Peter", date: null },
];

const map = new Map();
array1.forEach((o) => map.set(o.id, o.date));

let result = array2.map((o) => ({ ...o, date: o.date ?? map.get(o.id) }));
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

2) You can easily achieve the result using map and find

let array1 = [
  { id: 1, date: "23/11/21" },
  { id: 2, date: "20/11/21" },
  { id: 3, date: "15/11/21" },
];
let array2 = [
  { id: 1, name: "John", date: null },
  { id: 2, name: "Max", date: null },
  { id: 3, name: "Peter", date: null },
];

let result = array2.map(o => ({ ...o, date: o.date ?? array1.find(obj => obj.id === o.id)?.date}));
console.log(result)
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

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