我正在使用Angular和NodeJS创建图表(ngx-d3-线),我从NodeJS API中提取信息,查询结果如下
`[
{
"1": 15147,
"2": 37079,
"3": 28077,
"4": 20281,
"5": 18246,
"name": "DEV--ACCEPTED"
},
{
"1": 7,
"2": 36,
"3": 30,
"4": 21,
"5": 164,
"name": "DEV--WAITING"
},
{
"1": 6765,
"2": 20423,
"3": 14842,
"4": 10339,
"name": "DEV--REJECTED"
}
]`
但是我正在寻找的输出是
`[{name: 'DEV-REJECTED',
series: [
{
name: '1',
value: 6765,
},
{
name: '2',
value: 20423,
},
],
},
{
name: 'DEV--WAITING',
series: [
{
name: '1',
value: 7,
},
{
name: '2',
value: 36,
},{
name: '3',
value: 30,
},
],
}
}]`
关于如何获得输出的任何想法?
答案 0 :(得分:0)
您可以通过
修改jsonconst final_result =[]; // final out put
data.forEach( v => { // iterate your data
let d = {series: []}; // decorate your object;
d.name = v.name;
delete v.name; // delete name as assigned in other object
for (let k in v){ // iterate your object
d.series.push({name:k, value:v[k]});
}
final_result.push(d);
});
答案 1 :(得分:0)
const finalData = data.map(entry => {
return {
name: entry.name,
series: Object.keys(entry).filter(key => key !== "name").map(key => {
// Filtered out the entries where the key is 'name'
return {
name: key,
value: entry[key],
}
})
}
});