Javascript 使用多个对象数组替换多个对象数组
我尝试用对象数组替换或覆盖对象数组,如下所示Javascript 使用多个对象数组替换多个对象数组,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,我尝试用对象数组替换或覆盖对象数组,如下所示 let arr = [{ status: "ok" }, { status: "ok" }, { status: "error" }], arr2 = [{ status: "error", msg: "im first msg", "more property": true }]; arr = arr.map(a => { let fullObj = arr2.find
let arr = [{
status: "ok"
}, {
status: "ok"
}, {
status: "error"
}],
arr2 = [{
status: "error",
msg: "im first msg",
"more property": true
}];
arr = arr.map(a => {
let fullObj = arr2.find(a2 => a2.status === a.status);
return fullObj ? fullObj : a;
});
console.log(arr); //working
属性状态为error的对象数组的长度在arr和arr2上始终相同。但如果我有多个对象数组,它将不起作用
let arr = [{
status: "ok"
}, {
status: "ok"
}, {
status: "error"
}, {
status: "error"
}],
arr2 = [{
status: "error",
msg: "im first msg",
"more property": true
}, {
status: "error",
msg: "im the second msg",
"more property": true
}];
您可以移动错误数组并将其作为项的值
让arr=[{status:“ok”},{status:“ok”},{status:“error”},{status:“error”}],
arr2=[{status:“error”,msg:“im first msg”,“more property”:true},{status:“error”,msg:“im the second msg”,“more property”:true}];
arr.forEach((a,i,aa)=>{
如果(a.status==='error'){
aa[i]=arr2.shift();
}
});
控制台日志(arr)代码>
作为控制台包装{max height:100%!important;top:0;}
应该输出什么?连接消息?这个呢?如果你需要用多个对象替换,你需要多个对象和一个条件,应该使用哪个对象。第三个错误应该怎么办?第三个错误是什么?@Weedoze用arr2的对象数组替换arr2的对象数组对我来说就像是一个黑客。我做了另一种方法:arr.filter(obj=>obj.status!==“error”).concat(arr2)
它更改数组中对象的顺序。