我有json数据,如下所示,我从服务器获取
result = [
{"transactions": 7, "products__name": "mark"},
{"transactions": 12, "products__name": "vicky"},
{"transactions": 30, "products__name": "daniel"},
{"transactions": 6, "products__name": "hurray "}]
现在我想在上面的json响应中添加额外的项目并更改词典中的键名,我希望结果采用以下格式
result = [
{ label: "mark", data: 7, color: "#4572A7"},
{ label: "vicky", data: 12, color: "#4572A7"},
{ label: "daniel", data: 30, color: "#4572A7"},
{ label: "hurray", data: 6, color: "#4572A7"},
];
我想用jquery
执行上述逻辑并完成以下操作
var array = [];
var dictionary = {};
$.each(result, function(key,val){
dictionary['label'] = val.products__name;
dictionary['data'] = val.revenue;
dictionary['color'] = '#4572A7';
console.log(dictionary);
array.push(dictionary)
});
console.log(array);
});
所以从上面的jquery循环代码,我可以循环遍历json数组并可以创建一个包含不同标签和项目的字典,但是当我将它推入new array
时,只有起始字典是像重复插入数组一样,在数组中显示4次
我的代码中有什么问题,有人可以让我知道为了将不同的dic项目推送到新阵列需要做些什么吗?