Can someone assist me with a data structure issue?
Here are the objects I am working with:
const elements = {
item1: {color: 'blue', name: 'Item One' },
item2: {color: 'green', name: 'Item Two' },
item3: {color: 'red', name: 'Item Three' },
}
const values = { id: 0, item1: 20, item2: 50, item3: 10 }
I am trying to create another object (elementsWithValues
) in this format:
const elementsWithValues = {
item1: {color: 'blue', name: 'Item One', value: 20 },
item2: {color: 'green', name: 'Item Two', value: 50 },
item3: {color: 'red', name: 'Item Three', value: 10 },
}
This is the code snippet I have attempted:
const elementsWithValues = [
...Object.entries(elements).map(([itemName, element]) => ({
[itemName]: { ...element, value: values[itemName] },
})),
]
However, I am currently getting this result:
[
{ item1: {color: 'blue', name: 'Item One', value: 20 } },
{ item2: {color: 'green', name: 'Item Two', value: 50 } },
{ item3: {color: 'red', name: 'Item Three', value: 10 } },
]