如何使用另一个JS数组替换JS数组属性值



我想用另一个JS数组替换JS数组属性值。我解释如下。

const arr1 = [
{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
}, 
{
code: "AAW",
dis: "cont2",
note: "Note for cont2"
}, 
{
code: "TTR",
dis: "cont5",
note: "Note for cont5"
}, 
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
}]
const new_array = [
{
"code": "AAW",
"dis": "cont2 new",
"note": "Note for cont2"
},
{
"code": "TTR",
"dis": "cont5",
"note": "New Note for cont5"
}]

预期输出:

[
{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
}, 
{
code: "AAW",
dis: "cont2 new",
note: "Note for cont2"
}, 
{
code: "TTR",
dis: "cont5",
note: "New Note for cont5"
}, 
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
}
]

我们需要检查所有arr1元素,其中new_arr.code等于arr1.code,并比较disnote的属性。如果arr1.dis不等于new_arr.dis,则arr1.dis值应替换为new_arr.dis。这与note的性质相同。

尝试的代码:

arr1.forEach(function(item1) {
var item2 = arr1.find(function (item2) {
return arr1.code === new_array.code;
});
})
console.log(arr1);

电流输出:

[
{
"code": "XXY",
"dis": "cont1",
"note": "Note for cont1"
},
{
"code": "AAW",
"dis": "cont2",
"note": "Note for cont2"
},
{
"code": "TTR",
"dis": "cont5",
"note": "Note for cont5"
},
{
"code": "MMN",
"dis": "cont10",
"note": "Note for cont10"
}
]

如何解决此问题?

new_array.forEach(o1 => { // for every replacement object
// find the corresponding object in the original array
const found = arr1.find(o2 => o2.code === o1.code); 
// if there is a corresponding object
if (found) {
// copy its properties over
found.dis = o1.dis;
found.note = o1.note;
}
})

请注意,这具有较差的O(n^2)时间复杂性,如果您有一个大型数据集,请考虑使用其他答案建议的更快查找集。

给定一个方法,该方法可以将原始项与一系列可能的更新合并:

const mergeItem = (item, arr, finder) =>{
var other = arr.find(x => finder(item,x));
if(other != null){
Object.entries(other).forEach(([key,value]) => item[key] = value);
}
return item;
}

合并两个阵列的代码相当简单

var result = arr1.map(item => mergeItem(item,new_array, (x,y) => x.code == y.code));

实例:

const arr1 = [{"code":"XXY","dis":"cont1","note":"Note for cont1"},{"code":"AAW","dis":"cont2","note":"Note for cont2"},{"code":"TTR","dis":"cont5","note":"Note for cont5"},{"code":"MMN","dis":"cont10","note":"Note for cont10"}];
const new_array = [{"code":"AAW","dis":"cont2 new","note":"Note for cont2"},{"code":"TTR","dis":"cont5","note":"New Note for cont5"}];


const mergeItem = (item, arr, finder) =>{
var other = arr.find(x => finder(item,x));
if(other != null){
Object.entries(other).forEach(([key,value]) => item[key] = value);
}
return item;
}
var result = arr1.map(item => mergeItem(item,new_array, (x,y) => x.code == y.code));
console.log(result);

创建一个具有第二个数组的对象,其中代码是在O(1(时间内查找元素的关键。因此,复杂性降低到O(n(时间。

const arr1 = [{
code: "XXY",
dis: "cont1",
note: "Note for cont1"
},
{
code: "AAW",
dis: "cont2",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "Note for cont5"
},
{
code: "MMN",
dis: "cont10",
note: "Note for cont10"
}
]
const new_array = [{
"code": "AAW",
"dis": "cont2 new",
"note": "Note for cont2"
},
{
"code": "TTR",
"dis": "cont5",
"note": "New Note for cont5"
}
]
const t = new_array.reduce((res, item) => {
res[item.code] = item
return res;
}, {})
arr1.forEach(item => {
const found = t[item.code]
if (found) {
item.dis = found.dis
item.note = found.note
}
})
console.log(arr1)

const mappedArray = arr1.map((x) => {
const filteredValue = new_array.filter((y) => y.code === x.code);
return filteredValue.length > 0 ? filteredValue[0] : x;
});

最新更新