In a React app, I have this type of data:
const data = {
open: [{
id: 1,
name: 'test 1'
}, {
id: 2,
name: 'test 2'
}],
close: [{
id: 3,
name: 'test 3'
}, {
id: 4,
name: 'test 4'
}]
}
I want to iterate over each object in the open and close arrays and return the existing data with additional type field. E.g.:
const data = {
open: [{
id: 1,
name: 'test 1',
type: 'alert'
}, {...],
close: [...]
}
I'm trying something like this, but it returns undefined instead of the altered data:
const expandData = (data) => {
return Object.keys(data).forEach((key) => {
return data[key].map((item, idx) => ({
...item,
type: 'alert'
}))
})
}
// call in another method
expandData(data) // the `data` above