const contacts = [
{contact: "mobileno", value: "123456789", entered: "03-02-2021"},
{contact: "email", value: "test1@test.com", entered: "03-02-2021"},
{contact: "mobileno", value: "123456789"entered: "03-03-2021"},
{contact: "mobileno", value: "558843721"entered: "03-04-2021"},
{contact: "email", value: "test2@test.com", entered: "03-05-2021"},
{contact: "email", value: "test2@test.com", entered: "03-06-2021"},
]
Assuming an array of objects as above. I would like to eliminate duplicates and if there are duplicates, delete the one with the older entered date. The basis for uniqueness should be the contact type and value.
If both the contact type, contact value and entered date is the same, retain the one with the lower index. Correct processing of the one above should be:
const contacts = [
{contact: "email", value: "test1@test.com", entered: "03-02-2021"},
{contact: "mobileno", value: "123456789"entered: "03-03-2021"},
{contact: "mobileno", value: "558843721"entered: "03-04-2021"},
{contact: "email", value: "test2@test.com", entered: "03-05-2021"},
]
Ang suggestions to do this in the most efficient way? thanks
Date format used here by the way is: mm/dd/yyyy
I have tried looking at similar questions here but none of them have the condition of using a latest date entered field.
Try lodash.uniqWith
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
console.log(_.uniqWith(objects, _.isEqual));
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>