js实现-循环遍历数组,取出想要的数据封装成对象数组

一、要实现的效果:

在这里插入图片描述
在这里插入图片描述

二、代码实现:

//获取购物车列表
	cartIndexFun() {
    
    
		var that = this
		this.$api.appPlateForm('POST', this.$url.cart_index, '', function(res) {
    
    
			that.cartList = res.data   //购物车列表数据
			console.log(that.cartList)
			
			// 获取到每个商品的id和数量
			var goods_info=[];
			for(var i=0;i<that.cartList.length;i++){
    
    
				var temp={
    
    "id":that.cartList[i]["id"],"num":that.cartList[i]["num"]};
				goods_info.push(temp);
			}
			 console.log('打印后台要的商品数据:',goods_info)
		})
	},

在这里插入图片描述

这样就ok了!

猜你喜欢

转载自blog.csdn.net/weixin_48596030/article/details/131391154