Imagine I have two basic objects and I want to combine their properties to create a third object. It works smoothly with this code:
(()=>{
const firstObj = {a: 2, b: 3}
const secondObj = {a: 100, c: 5}
return {...firstObj, ...secondObj}
})()
// { a: 100, b: 3, c: 5 }
However, things get tricky when I attempt to use .reduce
to create a new object based on secondObj. To keep it simple, let's say we have a reduce function that simply clones the secondObj:
let result = (()=>{
const firstObj = {a: 2, b: 3}
const secondObj = {a: 100, c: 5},
cloneObj = Object.entries(secondObj).reduce((acc, [key, value])=>Object.defineProperty(acc, key, {value}), {})
console.log(cloneObj) // {a: 100 c: 5}
return {...firstObj, ...cloneObj}
})()
console.log(result);// { a: 2, b: 3 }
I have a suspicion that my understanding of the .reduce
function is lacking. How can I solve this issue?