var obj1 = [{
"id": 980550455852,
"model": "XQG70-S1208FW",
"color": "白",
"invStatusName": "正品",
"bactualQty": 10947,
"brealyQty": 11000,
"bavailQty": 53
}, {
"id": 980550566221,
"model": "XQB70-C3006",
"color": "灰",
"invStatusName": "正品",
"bactualQty": 11,
"brealyQty": 6,
"bavailQty": -5
}];
var obj2 = [{
"price": "6666"
}, {
"price": "8888"
}];
上面是两个数组里面有对个对象,需求是把obj2中的每个价格和obj1里面的一一合并成一个对象,最后生成一个新的数组,是这种形式
[{
"id": 980550455852,
"model": "XQG70-S1208FW",
"color": "白",
"invStatusName": "正品",
"bactualQty": 10947,
"brealyQty": 11000,
"bavailQty": 53,
"price": "6666"
}, {
"id": 980550566221,
"model": "XQB70-C3006",
"color": "灰",
"invStatusName": "正品",
"bactualQty": 11,
"brealyQty": 6,
"bavailQty": -5,
"price": "8888"
}]
我现在是这样写的
var obj1 = [{
"id": 980550455852,
"model": "XQG70-S1208FW",
"color": "白",
"invStatusName": "正品",
"bactualQty": 10947,
"brealyQty": 11000,
"bavailQty": 53
}, {
"id": 980550566221,
"model": "XQB70-C3006",
"color": "灰",
"invStatusName": "正品",
"bactualQty": 11,
"brealyQty": 6,
"bavailQty": -5
}];
var obj2 = [{
"price": "6666"
}, {
"price": "8888"
}];
var obj3 = {};
Object.assign.apply(Object, [obj3].concat(obj1, obj2));
console.log(obj3);
结果成这样原来两个变成一个了,6666价格对应的对象没了
简洁的代码:
比较好理解的代码: