{{totalPrcie}}
2. 使用for in
// 2for in 用法
totalPrcie() {
let total = 0;
for (let i in this.books) {
total += this.books[i].price
}
return total
}
3. for of
// 3 for of 用法
totalPrcie() {
let total = 0;
for (let i of this.books) {
total += i.price
}
return total
}
4. forEach
// 4 forEach 用法
totalPrcie() {
let total = 0;
this.books.forEach(item => {
total += item.price
});
return total
}
5.map
// 5 map
totalPrcie() {
let total = 0;
this.books.map(item => {
total += item.price
})
return total
}
6.filter
//6 filter
totalPrcie() {
let total = 0;
this.books.filter(item => {
total += item.price
})
return total
}
7.reduce
totalPrcie() {
return this.books.reduce(function(total, item) {
return total + item.price
}, 0)
}
// 箭头函数写法
totalPrcie() {
return this.books.reduce((total, item) =>
total + item.price, 0)
}
},
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)