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

361
Views
JavaScript Array: Order Nested Object by Key

I have the following nested object. I would like to iterate through every item (latest.sentTime) and then sort the object itself by "sentTime". So that the most recent message is on top. How can I achieve this?

In the following example, "0" and "1" need to be swapped basically, since "1" has a more recent "sentTime".

enter image description here

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

0

  • Using Array#map, iterate over the array to get latest.sentTime
  • Using Array#sort and Date, sort the items

const arr = [ 
  { latest: { sentTime: '2022-02-05T19:15:32.000Z' } },
  { latest: { sentTime: '2022-02-06T22:12:00.000Z' } } 
];

const sentTimes = arr
  .map(({ latest }) => latest.sentTime)
  .sort((a, b) => new Date(b) - new Date(a));

console.log(sentTimes);

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!