I am faced with the following scenario where I need to filter an array of objects based on lineId, subFamily, and status.
My current code successfully filters based on lineId, but now I also need to include a condition for subFamilyId.
I have two specific conditions:
1) If the lineId is the same but the subfamily is different, both objects should be included in the result. For example - if there are two objects with lineId 2 but different subFamilies, both should be kept.
2) If the lineId is the same but the status is different (e.g. New and Submitted), only the object with the Submitted status should be kept. For example - if two objects have subfamily 03 and lineId 3 but different statuses, only the submitted status object should be included.
Can someone assist me in adding a condition for subFamilyId to my existing code?
Below is the sample response:
const data = [
{
"Subfamily": "01",
"lineId": "2",
"status": "Submitted"
},
{
"Subfamily": "02",
"lineId": "2",
"status": "Submitted"
},
{
"Subfamily": "03",
"lineId": "3",
"status": "Submitted"
},
{
"Subfamily": "03",
"lineId": "3",
"status": "New"
},
{
"Subfamily": "04",
"lineId": "4",
"status": "New"
}
];
Expected Output
output = [
{
"Subfamily": "01",
"lineId": "2",
"status": "Submitted"
},
{
"Subfamily": "02",
"lineId": "2",
"status": "Submitted"
},
{
"Subfamily": "03",
"lineId": "3",
"status": "Submitted"
},
{
"Subfamily": "04",
"lineId": "4",
"status": "New"
}
];
Here is my current working code:
removeDuplicatesObject(data){
const uniqueResponse = data.reduce((acc, item) => {
if (!acc[item.lineId] || item.status === "Submitted") {
acc[item.lineId] = item;
}
return acc;
}, {});
const uniqueResponseArray = Object.values(uniqueResponse);
return uniqueResponseArray;
}
Any assistance on this matter would be greatly appreciated.