使用多个对象数组替换多个对象数组 [英] replace multiple array of object using multiple array of object
本文介绍了使用多个对象数组替换多个对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试用对象数组替换或覆盖对象数组,如下所示:
I try to replace or overwrite array of object with array of object, like this
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
属性状态等于错误的对象数组的长度将始终是一样的在arr和arr2上。但是如果我有多个对象数组,它将不起作用。
The length of array of object which has property status equal to error will always be the same on arr, and arr2. But it will not work if I have multiple array of object
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
}];
推荐答案
您可以将错误数组移到
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.forEach((a, i, aa) => {
if (a.status === 'error') {
aa[i] = arr2.shift();
}
});
console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这篇关于使用多个对象数组替换多个对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文