I have an object:
let lastPosts = {
id1: {
date: new Date("10/10/2000 11:30:34"),
},
id2: {
date: new Date("10/10/2000 10:00:00"),
},
id3: {
date: new Date("10/10/2000 10:01:30"),
},
id4: {
date: new Date("11/10/2000 13:09:30"),
},
id5: {
date: new Date("8/10/2005 18:10:15"),
},
id6: {
date: new Date("11/10/2000 10:00:11"),
},
id7: {
date: new Date("10/10/2000 10:00:01"),
},
id8: {
date: new Date("15/10/2000 11:30:34"),
},
id9: {
date: new Date("7/10/2002 11:30:34"),
},
id10: {
date: new Date("8/10/2005 18:10:14"),
},
id11: {
date: new Date("11/10/2000 10:00:00"),
},
};
and I need to ensure that "lastPosts" only contains 9 posts (elements), deleting the remaining oldest posts.
function deleteOldestLastPosts(lastPosts) {
if (Object.keys(lastPosts).length > 9) {
// What can I do here?
}
}
So, that I have the following result:
{
id1: {
date: new Date("10/10/2000 11:30:34"),
},
id3: {
date: new Date("10/10/2000 10:01:30"),
},
id4: {
date: new Date("11/10/2000 13:09:30"),
},
id5: {
date: new Date("8/10/2005 18:10:15"),
},
id6: {
date: new Date("11/10/2000 10:00:11"),
},
id8: {
date: new Date("15/10/2000 11:30:34"),
},
id9: {
date: new Date("7/10/2002 11:30:34"),
},
id10: {
date: new Date("8/10/2005 18:10:14"),
},
id11: {
date: new Date("11/10/2000 10:00:00"),
},
}
Any ideas?
CodePudding user response:
Use a priority queue. Javascript does not have one in-built, but you can find a package that provides it, or write one from scratch.
Use a min priority queue to store the id_n
objects, using date
field as the criteria. As soon as size of queue becomes > 9, remove the element at top of priority queue. Then, take that removed element's id_n
and remove it from the current object (lastPosts
).
At the end, you will have latest 9 elements in priority queue. And your object (lastPosts
) will have latest 9 id_n
objects.
The time complexity will be O(n log k)
, n
is size of lastPosts
and k is 9
.
CodePudding user response:
Sort all the values by date. Remove the newest 9 elements from this sorted array, producing an array of the dates to delete. Then loop through the properties, removing it if it's in the array.
function deleteOldestLastPosts(lastPosts) {
let values = Object.values(lastPosts);
if (values.length > 9) {
let datesToDelete = values.map(el => el.date).sort((a, b) => a - b);
datesToDelete.splice(-9);
Object.entries(lastPosts).forEach(([key, {
date
}]) => {
if (datesToDelete.includes(date)) {
delete lastPosts[key];
}
});
}
}
let lastPosts = {
id1: {
date: new Date("10/10/2000 11:30:34"),
},
id2: {
date: new Date("10/10/2000 10:00:00"),
},
id3: {
date: new Date("10/10/2000 10:01:30"),
},
id4: {
date: new Date("11/10/2000 13:09:30"),
},
id5: {
date: new Date("8/10/2005 18:10:15"),
},
id6: {
date: new Date("11/10/2000 10:00:11"),
},
id7: {
date: new Date("10/10/2000 10:00:01"),
},
id8: {
date: new Date("15/10/2000 11:30:34"),
},
id9: {
date: new Date("7/10/2002 11:30:34"),
},
id10: {
date: new Date("8/10/2005 18:10:14"),
},
id11: {
date: new Date("11/10/2000 10:00:00"),
},
};
deleteOldestLastPosts(lastPosts);
console.log(lastPosts);
CodePudding user response:
Here's a function getlastPosts
that will:
- sort the json object by date descending
- return the first
n
elements of the json object usingfilter
by the index of entries
The function uses sort
, filter
, and reduce
on the array Object.entries() of the JSON object.
let lastPosts = {
id1: {
date: new Date("10/10/2000 11:30:34"),
},
id2: {
date: new Date("10/10/2000 10:00:00"),
},
id3: {
date: new Date("10/10/2000 10:01:30"),
},
id4: {
date: new Date("11/10/2000 13:09:30"),
},
id5: {
date: new Date("8/10/2005 18:10:15"),
},
id6: {
date: new Date("11/10/2000 10:00:11"),
},
id7: {
date: new Date("10/10/2000 10:00:01"),
},
id8: {
date: new Date("15/10/2000 11:30:34"),
},
id9: {
date: new Date("7/10/2002 11:30:34"),
},
id10: {
date: new Date("8/10/2005 18:10:14"),
},
id11: {
date: new Date("11/10/2000 10:00:00"),
},
};
function getlastPosts(posts, n) {
return Object.entries(posts)
.sort(([, a], [, b]) => b.date - a.date)
.filter((e, i) => i < n)
.reduce((r, [k, v]) => ({ ...r, [k]: v }), {})
}
// get first 3 elements
console.log(getlastPosts(lastPosts, 3));
See also Sorting object property by values