'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?
Solution 1:[1]
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);
Solution 2:[2]
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);
Solution 3:[3]
Sort the items by dateAdded then take the top 5, which should be the oldest
Solution 4:[4]
Solutions sorting the array will likely modify the order of elements in the array. A non–mutating method is to grab the value to sort on and the related index, that way you can get the required elements without modifying the source array, e.g.
// To generate random data
function genRandomData(num = 10) {
let result = [];
for (let i=0; i<num; i++) {
result.push(
{productID: i,
dateAdded: new Date(Date.now() + (Math.random() - 0.5) * 1e10).toISOString()}
);
}
return result;
}
// Return n oldest entries in data
function getOldest(n, data) {
let indexes = data.map((obj, i) =>
({index:i, date: obj.dateAdded})
).sort( // Oldest first
(a, b) => b.date.localeCompare(a.date)
);
return indexes.slice(0,n).map(obj => data[obj.index]);
}
// Random data from genRandomData()
let data = [
{
"productID": 0,
"dateAdded": "2022-01-23T17:41:20.135Z"
},
{
"productID": 1,
"dateAdded": "2022-01-13T14:35:25.296Z"
},
{
"productID": 2,
"dateAdded": "2022-01-14T20:39:27.210Z"
},
{
"productID": 3,
"dateAdded": "2022-01-30T16:50:00.163Z"
},
{
"productID": 4,
"dateAdded": "2022-02-28T22:04:53.106Z"
},
{
"productID": 5,
"dateAdded": "2022-01-12T03:08:29.874Z"
},
{
"productID": 6,
"dateAdded": "2022-02-18T07:13:25.202Z"
},
{
"productID": 7,
"dateAdded": "2022-04-10T17:35:30.277Z"
},
{
"productID": 8,
"dateAdded": "2022-01-29T11:43:22.832Z"
},
{
"productID": 9,
"dateAdded": "2022-03-18T06:29:28.833Z"
}
];
// Oldest 3 elements of data
console.log(getOldest(3, data))
Note that the objects in the original array are referenced by the returned array, so modifying them also modifies the originals.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bravo |
| Solution 2 | Nguy?n V?n Phong |
| Solution 3 | Ross Gatih |
| Solution 4 | RobG |
