js两个数组合并且去重且数量相加

 

createorder: function() {
		var _self = this;
		var data = [];
		for (var i = 0; i < _self.msglist.length; i++) {
			if (_self.msglist[i].medic_sum > 0) {
				console.log(_self.msglist[i].medic_sum)
				var obj = {};
				obj.warehouse_id = _self.msglist[i].warehouse_id;
				obj.goods_id = _self.msglist[i].goods_id;
				obj.medic_sum = _self.msglist[i].medic_sum;
				data.push(obj)
			}
		}
		console.log(JSON.stringify(data))   //取出有数量的数据[]
		//uni.removeStorageSync('classarr');
		//获取原来储存的数据
		let arr = uni.getStorageSync('classarr');
		if (arr == "") {
			arr = data;
		} else {
			arr = JSON.parse(arr)
			for (var k = 0; k < data.length; k++) {
				let ty = 0;
				for (var i = 0; i < arr.length; i++) {
					if (arr[i].goods_id == data[k].goods_id) {
						arr[i].medic_sum += data[k].medic_sum;
						ty = 1;
					}
				}
				if (ty == 0) {
					arr.push(data[k]);
				}
			}
		}
		console.log(JSON.stringify(arr))
		uni.setStorageSync('classarr', JSON.stringify(arr));//储存数据
}

 

你可能感兴趣的:(Function)