JavaScript怎么把两个数组⾥的多个对象⼀⼀对应合并?合并数组或者对象在数组或对象前⾯加…,是es6的新写法,然后数组的map⽅法会返回数组.
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 obj = obj1.map((item,index)=>{
return{...item,...obj2[index]};
});
console.log(obj);
es5⽅法
function assign(target, args){
if(target ===null)return;
if(Object.assign){
return Object.assign(target, args);
}else{
var _ =Object(target);
for(var j =1; j < arguments.length; j++){
var source = arguments[j];
if(source){
for(var key in source){
if(Object.prototype.hasOwnProperty.call(source, key)){ _[key]= source[key];
}
}
js合并两个数组}
}
return _;
}
};
var obj = obj1.map(function(o,index){
return assign(o,obj2[index]);
})
console.log(obj);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论