Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Cómo fusionar dos objetos que contienen las mismas claves pero diferentes valores

Quiero fusionar dos matrices que llevan objetos con las mismas claves, pero un objeto puede tener un valor, pero el otro objeto, que lleva la misma clave, puede tener un valor nulo. Quiero fusionar los dos para intentar reemplazar todos los valores nulos y tener un objeto final.

He escrito lo siguiente, pero por el momento, solo estoy indefinido como resultado final.

 const objectOne = [ { one: null, two: "Two", three: null } ]; const objectTwo = [ { one: "One", two: null, three: "Three" } ]; const compareObjects = (searchKey, searchValue) => objectTwo.map((retrieve) => Object.entries(retrieve).forEach(([retrieveKey, retrieveValue]) => { if (!searchValue) { objectOne.searchKey = retrieveValue; console.log(objectOne); } }) ); const newObject = objectOne.map((search) => Object.entries(search).forEach(([searchKey, searchValue]) => compareObjects(searchKey, searchValue) ) ); console.log(newObject);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Reduce parece más útil en este caso que forEach. Reduce construirá algo nuevo a partir de la entrada y lo devolverá.

 const objectOne = [ { one: null, two: "Two", three: null } ]; const objectTwo = [ { one: "One", two: null, three: "Three" } ]; const merged = objectOne.map((item, idx) => { return Object.entries(item).reduce((acc, [key, value]) => { acc[key] = value === null && objectTwo.length > idx ? objectTwo[idx][key] : value; return acc; }, {}) }); console.log(merged);

about 4 years ago · Juan Pablo Isaza Report

0

Un método que puede usar es crear una función que combine/fusione los dos diccionarios. Primero, creamos nuestro newObject:

 const newObject = [ ]

Luego, creamos nuestra función usando un enfoque O(n^2) y la llamamos:

 combine(objectOne,objectTwo) console.log(newObject) function combine(objectOne, objectTwo){ for (let [key1,value1] of Object.entries(objectOne[0])){ for (let [key2,value2] of Object.entries(objectTwo[0])){ if(key1 == key2){ if(value1 == null){ newObject.push({ key: key1, value: value2 }) } else{ newObject.push({ key: key1, value: value1 }) } } } } }

Esta es la siguiente salida:

 [ { key: 'one', value: 'One' }, { key: 'two', value: 'Two' }, { key: 'three', value: 'Three' } ]
about 4 years ago · Juan Pablo Isaza Report

0

Su mapeo de claves/entradas es bastante cercano. Sin embargo, podría ser más fácil hacer algo como esto:

 const objectOnes = [ { one: null, two: "Two", three: null }, { a: 123, b: undefined, }, ]; const objectTwos = [ { one: "One", two: null, three: "Three" }, { b: 456, } ]; function merge(a, b) { const result = { ...a }; // copy a for (const key in result) { // Let's say we want to pick the non-null/undefined value if (result[key] !== null && result[key] !== undefined) continue; result[key] = b[key]; // copy from b } return result; } const merged = objectOnes.map((obj, i) => merge(obj, objectTwos[i])); console.log(merged);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!