我有一个对象数组,需要对其进行更改才能在我的角度应用程序中插入图表。以下是我的尝试。我需要帮助以弄清楚如何更改当前代码以获得所需的输出。 我仍在学习javascript,并认为自己是学生。因此,任何解释或指导都将受到欢迎。
所需的输出与此类似。这是我的参考。
[
{
"name": "Seychelles",
"series": [
{
"value": 5452,
"name": "2016-09-19T10:32:55.756Z"
},
{
"value": 3412,
"name": "2016-09-17T10:51:42.303Z"
},
{
"value": 3270,
"name": "2016-09-12T22:19:48.181Z"
},
{
"value": 2061,
"name": "2016-09-22T14:10:00.900Z"
},
{
"value": 2717,
"name": "2016-09-21T01:34:27.855Z"
}
]
},
{
"name": "Andorra",
"series": [
{
"value": 4200,
"name": "2016-09-19T10:32:55.756Z"
},
{
"value": 5142,
"name": "2016-09-17T10:51:42.303Z"
},
{
"value": 5999,
"name": "2016-09-12T22:19:48.181Z"
},
{
"value": 3736,
"name": "2016-09-22T14:10:00.900Z"
},
{
"value": 5471,
"name": "2016-09-21T01:34:27.855Z"
}
]
},
{
"name": "Barbados",
"series": [
{
"value": 2332,
"name": "2016-09-19T10:32:55.756Z"
},
{
"value": 2341,
"name": "2016-09-17T10:51:42.303Z"
},
{
"value": 2488,
"name": "2016-09-12T22:19:48.181Z"
},
{
"value": 5246,
"name": "2016-09-22T14:10:00.900Z"
},
{
"value": 5871,
"name": "2016-09-21T01:34:27.855Z"
}
]
},
{
"name": "Peru",
"series": [
{
"value": 3122,
"name": "2016-09-19T10:32:55.756Z"
},
{
"value": 3925,
"name": "2016-09-17T10:51:42.303Z"
},
{
"value": 3268,
"name": "2016-09-12T22:19:48.181Z"
},
{
"value": 4939,
"name": "2016-09-22T14:10:00.900Z"
},
{
"value": 4336,
"name": "2016-09-21T01:34:27.855Z"
}
]
},
{
"name": "Romania",
"series": [
{
"value": 2169,
"name": "2016-09-19T10:32:55.756Z"
},
{
"value": 3850,
"name": "2016-09-17T10:51:42.303Z"
},
{
"value": 6793,
"name": "2016-09-12T22:19:48.181Z"
},
{
"value": 4827,
"name": "2016-09-22T14:10:00.900Z"
},
{
"value": 2696,
"name": "2016-09-21T01:34:27.855Z"
}
]
}
]
这是我的尝试,也是要转换的原始数据的示例。
function cleanData(data) {
const emp = [];
const keys = Object.keys(data[0]);
for (let i = 0; i < data.length; i++) {
if(keys[i] != 'date') {
let obj = {};
let obj2 = {};
obj["name"] = keys[i];
obj["series"] = [];
obj2["values"] = data[i][keys[i]]
obj2["name"] = data[i].date
obj["series"].push(obj2);
emp.push(obj);
}
}
console.log(emp);
}
const arr = [
{
"date": "2018-07-01",
"nb_uniq_visitors": 573,
"nb_visits": 621,
"nb_users": 50,
"nb_actions": 1102,
"max_actions": 23,
"bounce_count": 381
},
{
"date":"2018-07-02",
"nb_uniq_visitors": 243,
"nb_visits": 571,
"nb_users": 67,
"nb_actions": 1002,
"max_actions": 33,
"bounce_count": 452
},
{
"date": "2018-07-03",
"nb_uniq_visitors": 203,
"nb_visits": 98,
"nb_users": 66,
"nb_actions": 902,
"max_actions": 73,
"bounce_count": 432
}
];
cleanData(arr);
答案 0 :(得分:2)
尝试reduce
进入按名称索引的对象(对象的键),然后对结果调用Object.values
使其返回数组:
const arr=[{"date":"2018-07-01","nb_uniq_visitors":573,"nb_visits":621,"nb_users":50,"nb_actions":1102,"max_actions":23,"bounce_count":381},{"date":"2018-07-02","nb_uniq_visitors":243,"nb_visits":571,"nb_users":67,"nb_actions":1002,"max_actions":33,"bounce_count":452},{"date":"2018-07-03","nb_uniq_visitors":203,"nb_visits":98,"nb_users":66,"nb_actions":902,"max_actions":73,"bounce_count":432}];
const itemsByKey = arr.reduce((a, { date, ...obj }) => {
Object.entries(obj).forEach(([key, value]) => {
if (!a[key]) a[key] = { name: key, series: [] };
a[key].series.push({ name: date, value });
});
return a;
}, {});
console.log(Object.values(itemsByKey));