我从ajax电话回来了这个json:
{"datasets":[
{"data":[0,0,2,0,1,0,0,0,0,0,5,2], "service_ID": 1},
{"data":[0,0,0,0,0,0,0,0,0,0,1,3], "service_ID": 2},
{"data":[0,0,0,0,0,0,0,0,0,0,0,2], "service_ID": 3},
{"data":[0,0,0,0,0,0,0,0,0,0,0,4], "service_ID": 4},
{"data":[0,0,0,0,0,0,0,0,0,0,0,1], "service_ID": 5}
]
}
data
对象中的每个元素都是一月份的值,从1月到12月(12个元素的数组)
我需要在datasets
内循环以获取data
对象中的每个值并得到总和,在此示例中为:2+1+5+2+1+3+2+4+1 = 21
我已经尝试了
var total = 0;
$.each(datasets, function(index, data) {
$.each(data, function(index, month) {
$.each(month, function(index, value) {
total = total + value;
});
});
});
但我收到TypeError: invalid 'in' operand a
错误。
获得我需要的最佳方法是什么?
答案 0 :(得分:5)
使用reduce
obj.datasets.reduce( function( a, b ){ //
return a + b.data.reduce( (c, d) => c + d , 0); // 0 is the accumulator for data property of each item in the array
} , 0); //0 is the accumulator which is the final output
甚至更短
obj.datasets.reduce( ( a, b ) => a + b.data.reduce( ( c, d ) => c + d , 0) , 0 );
<强>演示强>
var obj = {"datasets":[
{"data":[0,0,2,0,1,0,0,0,0,0,5,2], "service_ID": 1},
{"data":[0,0,0,0,0,0,0,0,0,0,1,3], "service_ID": 2},
{"data":[0,0,0,0,0,0,0,0,0,0,0,2], "service_ID": 3},
{"data":[0,0,0,0,0,0,0,0,0,0,0,4], "service_ID": 4},
{"data":[0,0,0,0,0,0,0,0,0,0,0,1], "service_ID": 5}
]
};
var output = obj.datasets.reduce( function( a, b ){
return a + b.data.reduce( (c, d) => c + d , 0);
} , 0);
console.log( output );
答案 1 :(得分:2)
以下是对reduce
的快速解释。它的名字就是这里的线索,因为它基本上采用了一系列单独的值(一个数组)并将它们缩减为单个值。在数组中得到整数之和实际上是一个非常好的例子。
reduce
接受一个接受两个参数的回调 - 第一个是累加器,所有其他值都添加到的值,第二个是迭代中的当前值。这里我们还将累加器的初始值设置为0。
const arr = [1, 2, 3, 4];
const out = arr.reduce((acc, curr) => acc + curr, 0);
console.log(out); // 10
&#13;
在gurvinder的回答中(我已经更改了变量名称以使其更容易理解)所发生的一切都是发生了几次求和操作。简而言之,我们用reduce
迭代数据集数组并获取每个数据数组中的整数之和(再次使用reduce
),并将每个arrSum
添加到主累加器中,sum
。
obj.datasets.reduce((sum, obj) => {
return sum + obj.data.reduce((arrSum, int) => arrSum + int , 0);
}, 0);
但还有更多!
这是一个简单的函数,它返回两个数字的总和:
const sum = (x, y) => x + y;
console.log(sum(1, 2)); // 3
&#13;
现在,因为JavaScript是一种函数式语言(函数是第一类对象),我们可以使用该函数来分离reducer
的和函数。
const arr = [1, 2, 3, 4];
const sum = (acc, curr) => acc + curr;
const out = arr.reduce(sum, 0);
console.log(out);
&#13;
这有助于我们编写更多功能代码,并且通常允许更好的代码重用。
例如,这里重写了gurvinders代码:
const sum = (sum, value) => sum + value;
const out = data.datasets.reduce((total, obj) => {
return sum(total, obj.data.reduce(sum), 0);
}, 0);
希望这是对reduce
的有用介绍。
答案 2 :(得分:1)
试试这段代码:
var dataset = {"datasets":[
{"data":[0,0,2,0,1,0,0,0,0,0,5,2], "service_ID": 1},
{"data":[0,0,0,0,0,0,0,0,0,0,1,3], "service_ID": 2},
{"data":[0,0,0,0,0,0,0,0,0,0,0,2], "service_ID": 3},
{"data":[0,0,0,0,0,0,0,0,0,0,0,4], "service_ID": 4},
{"data":[0,0,0,0,0,0,0,0,0,0,0,1], "service_ID": 5}
]
}
var total = 0;
$.each(dataset['datasets'], function(index1, data) {
$.each(data['data'], function(index2, value) {
total = total + value;
});
});
console.log(total)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 3 :(得分:0)
var example = {"datasets":[
{"data":[0,0,2,0,1,0,0,0,0,0,5,2], "service_ID": 1},
{"data":[0,0,0,0,0,0,0,0,0,0,1,3], "service_ID": 2},
{"data":[0,0,0,0,0,0,0,0,0,0,0,2], "service_ID": 3},
{"data":[0,0,0,0,0,0,0,0,0,0,0,4], "service_ID": 4},
{"data":[0,0,0,0,0,0,0,0,0,0,0,1], "service_ID": 5}
]
}
var sum = 0;
example.datasets.forEach(set => {
set.data.forEach(data => {
sum += data;
})
})
console.log(sum) // 21