I have an array arr that needs to be cleaned up by removing duplicate objects with the same e_display_id
and e_type
as P
. In this scenario, only objects with status==='N'
should be considered.
Here is the input array arr:
let arr =
[ { e_type: "P", e_record_id: 33780, e_display_id: "EA-15-001", status: "Y" }
, { e_type: "P", e_record_id: 33744, e_display_id: "PE-14-016", status: "N" }
, { e_type: "P", e_record_id: 33386, e_display_id: "PE-14-016", status: "Y" }
, { e_type: "P", e_record_id: 420, e_display_id: "PE-14-911", status: "Y" }
, { e_type: "P", e_record_id: 421, e_display_id: "PE-14-911", status: "N" }
, { e_type: "R", e_record_id: 33386, e_display_id: "PE-14-016", status: "Y" }
, { e_type: "R", e_record_id: 33386, e_display_id: "PE-14-016", status: "Y" }
];
My approach includes:
I'm utilizing lodash methods to filter out objects with e_type
as P
, then identifying any duplicate e_display_id
. If duplicates exist, I am retaining only those with status
as N
.
let clonedPursuits = [...arr];
let myarr = _.filter(clonedPursuits, x => x.e_type === 'P');
const counts = _.countBy(myarr, 'e_display_id');
clonedPursuits = _.filter(myarr, x => counts[x.e_display_id] > 1);
const uniqueAddresses = Array.from(new Set(clonedPursuits.map(a => a.e_display_id)))
.map(id => {
return clonedPursuits.find(a => a.e_display_id === id && a.status === "N");
});
console.log(uniqueAddresses);
Expected Result:
[ { e_type: "P", e_record_id: 33780, e_display_id: "EA-15-001", status: "Y" }
, { e_type: "P", e_record_id: 33744, e_display_id: "PE-14-016", status: "N" }
, { e_type: "P", e_record_id: 421, e_display_id: "PE-14-911", status: "N" }
, { e_type: "R", e_record_id: 33386, e_display_id: "PE-14-016", status: "Y" }
, { e_type: "R", e_record_id: 33386, e_display_id: "PE-14-016", status: "Y" }
];
Current Output:
[ { e_type: "P", e_record_id: 33744, e_display_id: "PE-14-016", status: "N"}
, { e_type: "P", e_record_id: 421, e_display_id: "PE-14-911", status: "N"}
]