我的数据如下:
?latlng=[latitude,longitude]&key=[your_gmap_api_key]
我想将上述数据转换为:
[
{
name: "ABC",
points: [
{
timestamp: "2017/09/26",
value: 1
},
{
timestamp: "2017/09/27",
value: 2
},
]
},
{
name: "DEF",
points: [
{
timestamp: "2017/09/26",
value: 0
},
{
timestamp: "2017/09/27",
value: 3
},
]
}
]
我刚开始学习ES6并强调。试图应用下划线来转换,但不能成功。
答案 0 :(得分:0)
您可以通过以下方式执行此操作
let arr = [
{
name: "ABC",
points: [
{
timestamp: "2017/09/26",
value: 1
},
{
timestamp: "2017/09/27",
value: 2
},
]
},
{
name: "DEF",
points: [
{
timestamp: "2017/09/26",
value: 0
},
{
timestamp: "2017/09/27",
value: 3
},
]
}
]
let result = arr.reduce((a, b) => {
for(let element of b.points){
a[element.timestamp] = a[element.timestamp] || {};
let newObj = {};
newObj[b.name] = element.value;
Object.assign(a[element.timestamp], newObj);
}
return a;
}, {});
result = Object.keys(result).map(key => Object.assign(result[key], {timestamp : key}));
console.log(result);

答案 1 :(得分:0)
由于您没有共同努力,我将把这个答案集中在算法上,并将实现部分留给您。
{ name: valueAsPerDate}
的Ta-DA !!!