遇见场景:echart图表中后台返回我的数据,后台无法修改key值,但是echart渲染图表的时候,需要用
var m2R2Data= [
{value:335,name:"种类01 335"},
{value:310,name:"种类02 310"},
{value:234,name:"种类03 234"},
{value:154,name:"种类04 154"},
{value:335,name:"种类05 335"}, ];
这样的数组
后台返回我的数组如下:
personAlarm:[
0: {staticLibName: "VIP客户", staticCount: 1416}
1: {staticLibName: "公司员工", staticCount: 2269}
2: {staticLibName: "陌生人", staticCount: 7253}
3: {staticLibName: "黑名单", staticCount: 10}
]
convertKey (arr, key) {
let newArr = [];
arr.forEach((item, index) => {
let newObj = {};
for (var i = 0; i < key.length; i++) {
newObj[key[i]] = item[Object.keys(item)[i]]
}
newArr.push(newObj);
})
return newArr;
},
//在需要的时候调用
let ydata = this.convertKey(this.personAlarm, ['name', 'value']);
//这样得到的就是echart可以渲染的数据了~