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

141
Views
Get 5 oldest Objects from an array

I have an array of objects of the form:

{productID: '15', name: 'Pepsi', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z'}

I want to find the 5 oldest items by comparing the dateAdded strings. I have tried a few inefficient loops and failed with reduce(), but I feel there's a more efficient way. How can I accumulate the 5 oldest items?

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

0

a simple sort/slice will do it - the format of the date means you can just do a localeCompare in your sort callack ... `

const array = [
    {productID: '15',name: 'Pepsi',category: 'food/beverages',dateAdded: '2015-12-21T17:42:34Z'},
    {productID: '13',name: 'Coke',category: 'food/beverages',dateAdded: '2015-12-20T17:42:34Z'}
];
const top5 = array
    .sort(({dateAdded: a}, {dateAdded: b}) => a.localeCompare(b))
    .slice(0, 5);
console.log(top5);

about 4 years ago · Juan Pablo Isaza Report

0

You can Sort by datetime like below.

array.sort((firstEl, secondEl) => 
    new Date(firstEl.dateAdded).getTime() - new Date(secondEl.dateAdded).getTime())
    .slice(0, 5);

 const array = [
      { productID: '0', name: 'Pepsi', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
      { productID: '1', name: 'Coke', category: 'food/beverages', dateAdded: '2015-10-20T17:42:34Z' },
      { productID: '2', name: 'Coke', category: 'food/beverages', dateAdded: '2015-11-20T17:42:34Z' },
      { productID: '3', name: 'Coke', category: 'food/beverages', dateAdded: '2015-08-20T17:42:34Z' },
      { productID: '4', name: 'Coke', category: 'food/beverages', dateAdded: '2015-01-20T17:42:34Z' },
      { productID: '5', name: 'Coke', category: 'food/beverages', dateAdded: '2015-03-20T17:42:34Z' },
      { productID: '6', name: 'Coke', category: 'food/beverages', dateAdded: '2016-10-20T17:42:34Z' },
      { productID: '7', name: 'Coke', category: 'food/beverages', dateAdded: '2017-10-20T17:42:34Z' },
    ];
    
const newArr = array.sort((firstEl, secondEl) => new Date(firstEl.dateAdded).getTime() - new Date(secondEl.dateAdded).getTime()).slice(0, 5);
    
console.log(newArr);

about 4 years ago · Juan Pablo Isaza Report

0

Sort the items by dateAdded then take the top 5, which should be the oldest

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!