该数组格式如下,数组里面是对象,对象里面有数组,看起来复杂,实际上并不难。
1 waitPayShow: [ 2 { 3 orderid: 0, 4 goods:[{ 5 goodid: 0, 6 price: 10, 7 number: 2 8 },{ 9 goodid: 1, 10 price: 10, 11 number: 2 12 }] 13 }, 14 { 15 orderid: 2, 16 [{ 17 goodid: 2, 18 price: 10, 19 number: 2 20 },{ 21 goodid: 3, 22 price: 10, 23 number: 2 24 }] 25 } 26 ] 27 28 /** 29 * 显示待付款页面 30 */ 31 waitPayShow: function () { 32 //创建数组 33 let array = [] 34 for (let i = 0; i < this.data.waitPayOrder.length; i++) { 35 let sumPrice = 0 36 let sumNumber = 0 37 for (let j = 0; j < this.data.waitPayOrder[i].goods.length; j++) { 38 let goodsPrice = parseFloat(this.data.waitPayOrder[i].goods[j].price) 39 let goodsNumber = parseFloat(this.data.waitPayOrder[i].goods[j].number) 40 sumPrice += goodsPrice * goodsNumber 41 sumNumber += goodsNumber 42 } 43 //将计算好的内容加到数组对象中 44 array.push( 45 Object.assign({}, this.data.waitPayOrder[i], { totalNumber: sumNumber, totalPrice: sumPrice }) 46 ) 47 } 48 this.setData({ 49 waitPayOrder: array 50 }) 51 },
如果有大佬发现哪里不妥欢迎指正。qq:1534147975.