【发布时间】:2019-04-15 04:37:00
【问题描述】:
我确定我只是有一点时间,但我什么都想不通。我想创建一个快速函数来总结数组中特定 JSON 对象的值,但它什么也不返回。代码如下:
var a=[{"b":"23"},{"b":"37"}]
function sumJSON(json,elem){
var total=0;
$.each(json,function(index,item){
var count=index+1;
total+=Number(item[elem]);
if(count===json.length){
return total;
}
})
}
console.log(sumJSON(a,"b"));
这是jsfiddle
【问题讨论】:
标签: javascript jquery arrays json function