I have an object array of an array in object. I would like to remove duplicate objects inside an object array of the object array.
var arr = {
"departsObj": {
"departments": [
{
"department": [
{
"groupID": "21",
"groupName": "group21",
"requestDate": "2020-01-24",
},
{
"groupID": "28",
"groupName": "group28",
"requestDate": "2020-01-24",
}
]
},
{
"department": [
{
"groupID": "28",
"groupName": "group28",
"requestDate": "2020-01-24",
},
{
"groupID": "20",
"groupName": "group20",
"requestDate": "2020-01-24",
},
{
"groupID": "30",
"groupName": "group30",
"requestDate": "2020-01-24",
}
]
}
]
}
}
I need to return the array :
{
"departsObj": {
"departments": [
{
"department": [
{
"groupID": "21",
"groupName": "group21",
"requestDate": "2020-01-24",
},
{
"groupID": "28",
"groupName": "group28",
"requestDate": "2020-01-24",
}
]
},
{
"department": [
{
"groupID": "20",
"groupName": "group20",
"requestDate": "2020-01-24",
},
{
"groupID": "30",
"groupName": "group30",
"requestDate": "2020-01-24",
}
]
}
]
}
}
I have tried :
const arr = departsObj.departments;
var result = arr.reduce((unique, o) => {
if(!unique.some(obj => obj.department === o.department)) {
unique.push(o);
}
return unique;
},[]);
return result;
But I still return duplicate objects. I am struggling to return the value from an object array of the array objects.
Any help would be appreciated.
Thanks in advance.