普通的for循环:
for (let i = 0; i < this.items.length; i++) {
totalPrice += (this.items[i].price) * (this.items[i].count);
}
for in 循环,通过索引去拿到值
for (let i in this.items) {
totalPrice += (this.items[i].price) * (this.items[i].count);
}
for of 循环,直接取到数组里面的对象
for (let item of this.items) {
totalPrice += item.price * item.count;
}

本文介绍了在JavaScript中计算总价时,使用普通for循环、for...in循环以及for...of循环的不同方式。for循环适用于按索引遍历,for...in通常用于遍历对象属性,而for...of则直接遍历数组元素。
762

被折叠的 条评论
为什么被折叠?



