【发布时间】:2016-04-23 04:38:38
【问题描述】:
我尝试同时使用 map 和 reduce 来构建一个函数,在该函数中循环遍历对象数组并进行一些数学运算,但我得到了 NAN。为什么?
function getTotal(){
var obj = [
{
"name": "item 1",
"discount_price": 86.9,
"qty": 1,
},
{
"name": "item 2",
"discount_price": 11.9,
"qty": 1,
}
];
return obj.map(function(x){;return x.discounted_price * x.qty}).reduce(function(a,b){return a + b});
}
$('p').text(getTotal());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<p></p>
【问题讨论】:
标签: javascript jquery