I want to replace JS array property values using another JS array. I have explained as below.
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"
}]
Expected output:
[
{
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"
}
]
We need to check all arr1 element where equal new_arr.code to arr1.code and compare dis and note properties. If arr1.dis not equals new_arr.dis then arr1.dis value should be replaced by new_arr.dis. This is same to note property also.
Tried code:
arr1.forEach(function(item1) {
var item2 = arr1.find(function (item2) {
return arr1.code === new_array.code;
});
})
console.log(arr1);
Current output:
[
{
"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"
}
]
How can I fix this problem?