发表于: 2020-06-25 20:56:11

1 2141


今日完成

for遍历的写法

// //方法一,普通for循环
// let totalPrice = 0 //最终价格
// for (let i = 0; i < this.books.length; i++) {
//     totalPrice += this.books[i].price * this.books[i].count
//     //某一栏的价格,乘以相对应的数量
// }
// return totalPrice

//方法二,for (let i in this.books)
//    let totalPrice = 0
//    for(let i in this.books){
//     //    console.log(i);//i 是索引值
//     totalPrice += this.books[i].price * this.books[i].count

//    }
// //    return 1//不回调,报错函数未定义
// return totalPrice

// // 方法三,for (let i of this.books)
// let totalPrice = 0
// for(let item of this.books){
//     //拿到的直接就是数组的某一项
//     totalPrice += item.price*item.count
// }
// return totalPrice

// 方法四,reduce()
return this.books.reduce(function (pre, book) {
// totalPrice = pre + n
return pre + book.price * book.count
}, 0)

}



返回列表 返回列表
评论

    分享到