预期
const saveStatistics = [
{ name: '已关闭', value: [900, 1060, 1100, 1100, 1200, 1000, 1200] },
{ name: '已处理', value: [1200, 1620, 1320, 1530, 2640, 1350, 1300] },
{ name: '待处理', value: [220, 400, 120, 130, 540, 150, 1740] }
],
//需要处理的数据
const obj = [
{"已关闭": [12,23,4,5,6,]},
{"已处理": [1,8,0,5,4,]},
{"待处理": [4,5,1,5,6,]},
]
怎么把下面的数据转换成预期的数据
var arrs=new Array();
const obj = [
{"已关闭": [12,23,4,5,6,]},
{"已处理": [1,8,0,5,4,]},
{"待处理": [4,5,1,5,6,]},
];
obj.forEach(function(e,i){
var item = Object.getOwnPropertyNames(e);
var info={ name:item[0],value:e[item] }
arrs.push(info);
});
console.log(arrs);
const saveStatistics = [
{ name: '已关闭', value: [900, 1060, 1100, 1100, 1200, 1000, 1200] },
{ name: '已处理', value: [1200, 1620, 1320, 1530, 2640, 1350, 1300] },
{ name: '待处理', value: [220, 400, 120, 130, 540, 150, 1740] }
];
var obj=[];
saveStatistics.forEach(x=>{
obj[x.name]=x.value;
});
console.log(obj);
搞错了,兄弟,这是预期的
let saveStatistics = obj.map(item => {
let arr = Object.entries(item)[0];
return {
name: arr[0],
value: arr[1]
};
});
console.log(saveStatistics);
js?
– echo_lovely 2年前