I have this structure
const raw = [{
date: '2018-8-1',
devices: [{
id: 1,
name: 'iphone',
device_age: 12
}, {
id: 2,
name: 'samsung',
device_age: 10
}]
}, {
date: '2018-8-2',
devices: [{
id: 1,
name: 'iphone',
device_age: 5
}, {
id: 2,
name: 'samsung',
device_age: 9
}]
}]
I want to transform above sturcture into this
[{date: '2018-8-1', iphone: 12, samsung: 10}, {date: '2018-8-2', iphone:5, samsung:9}]
What's wrong with my attempt below?
let result = raw.map((obj, i) => {
return {
date: obj.date,
[obj.devices.map(innerObj => innerObj.name)]: 'test'
}
})
https://jsfiddle.net/q74x8106/
or I shouldn't use map for inner loop? I'm lost.