map与reduce结合计算数组对象属性值

let detail = [
  {name:'上衣',price:110},
  {name:'衬衣',price:345},
  {name:'裤子',price:678},
  {name:'帽子',price:45}
]
function totalPrice(){
   return this.detail.map(item=>item.price).reduce((x,y)=>x+y,0);
}

其他方法

totalPrice2() {
  let total = 0;
  for (let item of this.detail) {
    total += item.price;
  }
  return total;
},
totalPrice3() {
  let total = 0;
  for (let i in this.detail) {
    total += this.detail[i].price;
  }
  return total;
},
totalPrice4() {
  let total = 0;
  this.detail.forEach(item => total += item.price);
  return total;
}

你可能感兴趣的:(数据处理)