I want to filter data from the JS array and create another JS array. I explained the scenario as below.
There are two JS arrays. they are arr1 and arr2
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 arr2 = [
{
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"
},
{
code: "vvg",
dis: "cont15",
note: "Note for cont15"
}];
we can do change in arr2[1].dis and arr2[2].note and arr2[4].note. I have mentioned it in below
const arr2 = [
{
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"
},
{
code: "VVG",
dis: "cont15",
note: "Note for cont15 changed"
}];
Once I changed the arr2 I want to compare it with arr1 and create new_arr and push all the changed elements to the new_arr. Compare should be done with code. Because code: "VVG" has a change. But it should not be contained a new array. Because code: "VVG" not in arr1.
Expected-output :
[
{
code: "AAW",
dis: "cont2 new",
note: "Note for cont2"
},
{
code: "TTR",
dis: "cont5",
note: "New Note for cont5"
}
]
Tried code:
const codesss = new Set(arr1.map(({ code }) => code));
const new_arr = arr2.filter(({ code }) => !codesss.has(code));
console.log(new_arr);
current output:
[
{
"code": "VVG",
"dis": "cont15",
"note": "Note for cont15 changed"
}
]
My current output is not the Expected-output. Please help me to solve this.