const customer = [ {id: 1, count: 2}, {id: 2, count: 89}, {id: 3, count: 1}];转换成:const customer = [ {type:'id',value:[1,2,3]}, {type:'count',value:[2,89,1]},];//customer是动态数据,id和count并不是固定的解决方案可参考sxlwar回答内他的评论。
1 回答

呼啦一阵风
TA贡献1802条经验 获得超6个赞
const middle = customer.reduce((acc, cur) => {
Object.keys(cur).forEach(key => {
if(acc[key]){
acc[key].push(cur[key]);
}else {
acc[key] = [cur[key]];
}
});
return acc;
},{});
const r = Object.keys(middle).map(key => ({type: key, value: middle[key]}));
// or
const result = Object.keys(middle).reduce((acc,cur) => {
acc.push({type: cur, value: middle[cur]});
return acc;
}, []);
添加回答
举报
0/150
提交
取消