我是javaScript新手并做出反应..
我有一个由对象组成的数组。我需要在AG GRID中显示这个数组,所以我在这里将它映射到flat obj的数组。
注意:我不能使用jQuery ..
下面是我的数组(完整代码在codepen中,请检查链接)
let realData = [
[
{
"key" : "flightNumber" ,
"label": "Flight Number",
"value": "EK 131",
"value-type": "String",
"order" : 1
},
{
"key" : "flightId",
"label": "flightId",
"value": "1223434",
"value-type": "String",
"order" : 2,
"isId": true
},
{
"key": "CallSign",
"label": "callSign",
"value": "123",
"value-type": "String",
"order" : 3
},
]
]
我想要的结果是
let newArray = [
{flightNumber: 'EK 131', flightType: 'A', lastUpdate: '11223232', destOrig: 'BEY', sibtSobt: '12121333123', CallSign: '123'},
{flightNumber: 'EK 132', flightType: 'D', lastUpdate: '11334455', destOrig: 'DEST', sibtSobt: '1234443222', CallSign: '443'}
]
请帮助
谢谢
答案 0 :(得分:2)
您可以reduce
将每个数组键/值对放入对象中:
const arrays = [
[
{
"key" : "flightNumber" ,
"label": "Flight Number",
"value": "EK 131",
"value-type": "String",
"order" : 1
},
{
"key" : "flightId",
"label": "flightId",
"value": "1223434",
"value-type": "String",
"order" : 2,
"isId": true
},
{
"key": "CallSign",
"label": "callSign",
"value": "123",
"value-type": "String",
"order" : 3
}
]
];
const objects = arrays.map(arr => arr.reduce((acc, cur, i) => {
acc[cur.key] = cur.value;
return acc;
}, {}));
console.log(objects);