let total = 0
this.data.list2.map(res=>{
if(res.checked){
total+=res.money
}
})
改成reduce的写法
我下面这样写的结果是空 哪里写的不对吗let sum = this.data.list2.reduce((pre,next)=>pre.checked?pre.money+next.money:'')
let total = 0
this.data.list2.map(res=>{
if(res.checked){
total+=res.money
}
})
改成reduce的写法
我下面这样写的结果是空 哪里写的不对吗let sum = this.data.list2.reduce((pre,next)=>pre.checked?pre.money+next.money:'')
this.data.list2.reduce((acc, cur) => acc + cur.money * cur.checked, 0)
自认为这样比三目帅(当然你的 checked
要保证合法,如果可能是 undefined
就改成 !! cur.checked
)
const total = this.data.list2.reduce((result,current) => pre.checked ? (result + pre.money) : result, 0)
let sum = this.data.list2.reduce((pre, next) => (next.checked && (pre += next.money), pre), 0)
由于原生的 filter、map、reduce 都会进行遍历,所以数据量越大,后面的解决方案效率越低。大量数据可以考虑使用 Lodash 的 Lazy 运算(多大数据算大数据,最好实测一下效率比较)