我有一个返回数组的 json 响应。我想从数组中计算 json 数组中变量的总数。这是一个片段
$rootScope.getData = function () {
$http({
method: 'GET',
....
console.log(JSON.stringify(res.data.data));
returns
[{
"name":"John",
"age":30
}
{
"name":"Doe",
"age":30
}]
如何计算数组中的总年龄得到 60 是一个挑战
最佳答案
使用折叠/减少
res.data.data.reduce(function (total, person) {
return total + person.age;
}, 0);
关于javascript - 从 json 数组计算变量的总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53450735/