我正在运行查询并使用表示特定组中人数的值返回JSON。我的数据回复如下:
0: Object
count: 10
grp: 1
1: Object
count: 20
grp: 2
2: Object
count: 30
grp: 3
3: Object
count: 40
grp: 4
我想得到的是运行总数并保存在一个数组中,所以我最终会得到这样的结果:
[[0,10],[1,30],[2,60],[3,100]]
这是我的开始,但不确定我需要在push
中添加什么。
d1_1 = [];
$.each(data.rows, function(index, value){
d1_1.push(***what goes here?***);
});
答案 0 :(得分:2)
这可以解决这个问题吗?
var input = [{count:10, grp:1},{count:20,grp:2},{count:30,grp:3},{count:40,grp:4}];
counter = 0;
var d1_1 = [];
jQuery.each(input, function(index, elem) {
counter += elem.count;
d1_1.push([index,counter]);
});
答案 1 :(得分:0)
var json = [{count: 10, group: 1},{count: 20, group: 2}, {count: 30, group: 3},{count: 40, group: 4}];
var myJSONArray = [];
var myArray = [];
$.each(json, function(index, value){
//Results in [{count: 10, group: 1}, {count: 20, group: 2}, etc]
var temp = {"count": value.count, "group": value.group};
myJSONArray.push(temp);
//console.log(myJSONArray);
//Results in [10, 1], [20, 2], etc
myArray.push([value.count, value.group]);
//console.log(myArray);
});
如果你想将索引推入你的数组,你可以像这样做
var json = [{count: 10, group: 1},{count: 20, group: 2}, {count: 30, group: 3},{count: 40, group: 4}];
var myArray = [];
$.each(json, function(index, value){
//Results in [0, 10], [1, 20], etc
myArray.push([index, value.count]);
//console.log(myArray);
});
答案 2 :(得分:0)
var json = [{count: 10,grp: 1}, {count: 11,grp: 2}],
result = [],
temp = [];
for (var i in json) {
temp.push(json[i].grp, json[i].count);
result.push(temp);
temp = [];
}