In my Angular application, I'm attempting to retrieve only the updated array that was modified from the user interface.
Within this scenario, I have two arrays in which one of the role names has been changed. My goal is to compare and extract the modified object, returning only those arrays that have been modified (specifically, where the role_name values were altered).
I've made an attempt at filtering using ES6 methods, but it doesn't seem to be functioning correctly.
Could someone assist me in identifying where I may be making a mistake?
var arr1 = [ {
"num" : null,
"role_name" : "ABC",
"profile" : "ff",
"user" : "1234",
"rn" : "1",
"user_name" : "Rywan"
},
{
"num" : null,
"role_name" : "DEF",
"profile" : "ff",
"user" : "1234",
"rn" : "2",
"user_name" : "adecg"
},
{
"num" : null,
"role_name" : "GHJ",
"profile" : "ff",
"user" : "1234",
"rn" : "3",
"user_name" : "dde"
},
{
"num" : null,
"role_name" : "RRT",
"profile" : "ff",
"user" : "1234",
"rn" : "4",
"user_name" : "kumar"
},
{
"num" : null,
"role_name" : "SFR",
"profile" : "ff",
"user" : "1234",
"rn" : "5",
"user_name" : "SASI"
}
];
var arr2 = [ {
"num" : null,
"role_name" : "ABC",
"profile" : "ff",
"user" : "1234",
"rn" : "1",
"user_name" : "Rywan"
},
{
"num" : null,
"role_name" : "ROLE_CHANGED",
"profile" : "ff",
"user" : "1234",
"rn" : "2",
"user_name" : "adecg"
},
{
"num" : null,
"role_name" : "GHJ",
"profile" : "ff",
"user" : "1234",
"rn" : "3",
"user_name" : "dde"
},
{
"num" : null,
"role_name" : "RRT",
"profile" : "ff",
"user" : "1234",
"rn" : "4",
"user_name" : "kumar"
},
{
"num" : null,
"role_name" : "SFR",
"profile" : "ff",
"user" : "1234",
"rn" : "5",
"user_name" : "SASI"
}
];
let filteredData;
filteredData = arr1.filter(function(o1){
//filter out (!) items in arr2
return arr2.some(function(o2){
return o1.role_name !== o2.role_name;
});
});
console.log(filteredData);
The desired output should be:
[
{
"num" : null,
"role_name" : "ROLE_CHANGED",
"profile" : "ff",
"user" : "1234",
"rn" : "2",
"user_name" : "adecg"
}
]