Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
NodeJS (ES-6) - removing duplicates from array of objects using latest one
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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!