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?
CodePudding user response:
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);
CodePudding user response:
Sort the items by dateAdded
then take the top 5, which should be the oldest
CodePudding user response:
Sort by datetime
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)
CodePudding user response:
const oldest = (arr) => arr.sort((a, b) => new Date(a.dateAdded) - new Date(b.dateAdded)).slice(0, 5)
const products = [
{ productID: '15', name: 'Pepsi', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '16', name: 'Coke', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '17', name: 'Sprite', category: 'food/beverages', dateAdded: '2014-12-21T17:42:34Z' },
{ productID: '18', name: 'Fanta', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '19', name: 'Dr Pepper', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '20', name: 'Mountain Dew', category: 'food/beverages', dateAdded: '2012-12-21T17:42:34Z' },
{ productID: '21', name: 'Diet Coke', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '22', name: '7up', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '23', name: 'Cherry Coke', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '24', name: 'Cherry Coke Zero', category: 'food/beverages', dateAdded: '2010-12-21T17:42:34Z' },
{ productID: '25', name: 'Cherry Coke Diet', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '26', name: 'Cherry Coke Diet Zero', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '27', name: 'Cherry Coke Diet Zero', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '28', name: 'Cherry Coke Diet Zero', category: 'food/beverages', dateAdded: '2011-12-21T17:42:34Z' },
{ productID: '29', name: 'Cherry Coke Diet Zero', category: 'food/beverages', dateAdded: '2015-12-21T17:42:34Z' },
{ productID: '30', name: 'Cherry Coke Diet Zero', category: 'food/beverages', dateAdded: '2014-12-21T17:42:34Z' }]
console.log(oldest(products));