我有以下包含对象的数组,我想总结所有出现的情况watt
:
let allProducts = [{
"unique_id": "102",
"currency": "$",
"price": "529.99",
"watt": 150
},
{
"unique_id": "11",
"currency": "$",
"price": "323",
"watt": 150
},
{
"unique_id": "13",
"currency": "$",
"price": "23",
"watt": 77
}
]
let getWatt =
_(allProducts)
.map((objs, key) => ({
'watt': _.sumBy(objs, 'watt')
}))
.value()
console.log(getWatt)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
正如你所看到的,我返回了一系列0
价值观。但是,我想取回结果值377
。有什么建议我做错了什么吗?
我很感谢你的回复!
使用纯js很容易
const sum = allProducts.reduce((a, {watt}) => a + watt, 0);
console.log(sum);
<script>
let allProducts = [{
"unique_id": "102",
"currency": "$",
"price": "529.99",
"watt": 150
},
{
"unique_id": "11",
"currency": "$",
"price": "323",
"watt": 150
},
{
"unique_id": "13",
"currency": "$",
"price": "23",
"watt": 77
}
]
</script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)