Desired result: Insert an object into each dynamicItem and have the full obj object.
const obj = {
item1: {},
item2: {
dynamicItem1: [{ id: 'one' }, ], // add another prop here {type: 'added'}
dynamicItem2: [{ id: 'one' }, ] // add another prop here {type: 'added'}
},
}
My attempts: I managed to insert the object, but i can't think how to rebuild the full object and it also feels like i'm doing it wrong already.
const result = Object.keys(obj.item2).map(key => {
return obj.item2[key].map(item => ({ ...item, type: 'added' }))
})
const res = {...obj, item2: Object.fromEntries(Object.entries(obj.item2).map(([k,v]) => ([k, [...v].concat([{type: 'added'}])])))};. Specifically what errors or issues are faced. And, as @Andreas has noted, SO has all of these ideas in various questions. :-)