I am getting below response, i wanted to remove all the duplicates object based on lineId and status. I am trying to find all the duplicates with new status in below response.
In the below example, i wanted to remove those objects whose lineId is same but status is New.
i tried below code but unable to filter the expected result.
I appreciate any help on this.
const response = [
{
"line": "Line 2",
"lineId": "R_X002_WC02",
"status": "New"
},
{
"line": "Line 3",
"lineId": "R_X002_WC03",
"status": "New"
},
{
"line": "Line 2",
"lineId": "R_X002_WC02",
"status": "Submitted"
},
{
"line": "Line 4",
"lineId": "R_X002_WC04",
"status": "New"
},
{
"line": "Line 4",
"lineId": "R_X002_WC04",
"status": "Submitted"
}
];
Below is my expected Output
const ExpectedOutput = [
{
"line": "Line 3",
"lineId": "R_X002_WC03",
"status": "New"
},
{
"line": "Line 2",
"lineId": "R_X002_WC02",
"status": "Submitted"
},
{
"line": "Line 4",
"lineId": "R_X002_WC04",
"status": "Submitted"
}
];
i tried below code
var finalarr = [];
response.forEach((item) => {
response.filter(i => i.lineId === item.lineId).length > 1
finalarr.push(item);
});
var finalarr = response.filter((item, i) => i === response.findLastIndex(other => other.lineId === item.lineId))