我有这个结构
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
}]
}]
我想把上面的结构转换成这个
[{date: '2018-8-1', iphone: 12, samsung: 10}, {date: '2018-8-2', iphone:5, samsung:9}]
我在下面的尝试有什么问题?
let result = raw.map((obj, i) => {
return {
date: obj.date,
[obj.devices.map(innerObj => innerObj.name)]: 'test'
}
})
https://jsfiddle.net/q74x8106/
或者我不应该使用map进行内循环?我输了。
答案 0 :(得分:2)
您可以映射外部数组并使用Object.assign
和spread syntax ...
构建新对象。
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 }] }],
result = raw.map(({ date, devices }) =>
Object.assign({ date }, ...devices.map(d => ({ [d.name]: d.device_age }))));
console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)
<DataGrid x:Name="dataGrid" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" ...>