1、第一种就是最基础的for循环,循环中的i为数组的下标,所以在下面例子中需要使用this.products[i]取值
totalPrice() {
var totalPrice=0;
for (var i =0;i<this.products.length;i++){
totalPrice+=this.products[i].shopPrice * this.products[i].count;
}
console.info(totalPrice);
return totalPrice;
}
2、第二种就是用for...in循环,循环中的i为数组的下标,所以在下面例子中需要使用this.products[i]取值
totalPrice() {
var totalPrice=0;
for (var i in this.products){
totalPrice+=this.products[i].shopPrice * this.products[i].count;
}
console.info(totalPrice);
return totalPrice;
}
3、第三种就是for...of循环,for...of循环中的i为数组的值,所以在下面例子中仅需使用 i 取值
totalPrice() {
var totalPrice=0;
for (var i of this.products){
totalPrice+=i.shopPrice * i.count;
}
console.info(totalPrice);
return totalPrice;
}
参考:for循环的多种用法 - 效率的九尾 - 博客园 (cnblogs.com)https://www.cnblogs.com/zhanghailing/p/12795821.html