vue js 小程序 将一个数组平分成两个新数组

1,490 阅读1分钟
//这里是监听数组变化,可以不监听
watch: {   allProduceArr(newVal, oldVal) {
   // console.log('新数组', newVal)
   this.sortArr(newVal); //这里会把新数组传到sortArr这个方法里面进行处理 }
},

// 将列表分成左右两列
async sortArr(arr) { 
	var array = arr; //将监听传过来的数据重新赋值给array
	array.map((item, index) => { //这里面处理我们需要处理的数据,不需要处理的话就可以省略
		item.DiscountPrice = item.DiscountPrice ? this.helper.toFixed(item.DiscountPrice) : 0;
		// item.Discount = item.Discount * 10
		item.NetPath = item.ImageUrl ? item.ImageUrl : '';
		item.GoodsCode = item.GoodsCode;
		item.MembershipPrice = item.MembershipPrice ? this.helper.toFixed(item.MembershipPrice) : 0;
		item.MarketPrice = item.MarketPrice ? this.helper.toFixed(item.MarketPrice) : 0;
		item.WeightShort = item.WeightShort ? item.WeightShort : '';
	});
	this.goodList = array; //我这里是将处理过的数据返回使用,不使用可以省略
	//将array进行处理,他的index索引余2===0的就放到一个新数组中leftArr
	this.leftArr = array.filter((_item, index) => index % 2 === 0);
	//将array进行处理,他的index索引余2 !===0的就放到一个新数组中rightArr
	this.rightArr = array.filter((_item, index) => index % 2 !== 0);
	// console.log('商品列表组件数据', this.leftArr, this.rightArr)
},