如何向对象里面添加元素,本例是向数组对象中加如元素,方法是一样的。

该数组格式如下,数组里面是对象,对象里面有数组,看起来复杂,实际上并不难。

 waitPayShow: [
   {
     orderid: 0,
     goods:[{
       goodid: 0, price: 10, number: 2 },{ goodid: 1, price: 10, number: 2 }] }, { orderid: 2, [{ goodid: 2, price: 10, number: 2 },{ goodid: 3, price: 10, number: 2 }] } ] /** * 显示待付款页面 */ waitPayShow: function () { //创建数组 let array = [] for (let i = 0; i < this.data.waitPayOrder.length; i++) { let sumPrice = 0 let sumNumber = 0 for (let j = 0; j < this.data.waitPayOrder[i].goods.length; j++) { let goodsPrice = parseFloat(this.data.waitPayOrder[i].goods[j].price) let goodsNumber = parseFloat(this.data.waitPayOrder[i].goods[j].number) sumPrice += goodsPrice * goodsNumber sumNumber += goodsNumber } //将计算好的内容加到数组对象中 array.push( Object.assign({}, this.data.waitPayOrder[i], { totalNumber: sumNumber, totalPrice: sumPrice }) ) } this.setData({ waitPayOrder: array }) }, 

如果有大佬发现哪里不妥欢迎指正。qq:1534147975.

猜你喜欢

转载自www.cnblogs.com/sinceForever/p/11363508.html
0条评论
添加一条新回复
  
今日推荐