当前位置:网站首页>Calculate the sum of an element of an array

Calculate the sum of an element of an array

2022-08-11 10:03:00 Huasheng_

let arr=[{name:'Zhang San',value:10},{name:'Li Si',value:11},{name:'Wang Wu',value:12},]let counts = 0for (let i = 0; i < arr.length; i++) {if (arr[i].value) {counts += arr[i].value}}console.log(counts) // 33

insert image description here
Or replace the code of the for loop with the following code,As shown
insert image description here

arr.map(res=>{return counts +=res.value})

There are other methods, welcome to add in the comment area~

原网站

版权声明
本文为[Huasheng_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208111000140209.html