for循环
forEach
reduce
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let list = [
{
id: 1,
price: 10,
},
{
id: 2,
price: 10,
},
{
id: 3,
price: 10,
},
{
id: 4,
price: 10,
},
]
let sum = 0
for (let i = 0; i < list.length; i++) {
sum += list[i].price
}
console.log('第一种:' + sum);
let sum2 = 0
list.forEach(v => {
sum2 += v.price
});
console.log('第二种:' + sum2);
let sum3
sum3 = list.reduce((sum3, obj) => (sum3 += obj.price), 0)
console.log('第二种:' + sum3);
</script>
</body>
</html>