Sebastian Olsen Sebastian Olsen - 1 year ago 65
Javascript Question

Get property with oldest date

I'm in a situation where I need to store some data in an object, but I can only have a set number of that data due to browser limitations. Since my app also needs to be able to get this data, I am storing it in an object where the keys of the properties are identifiers.

The data looks like this:

memory = {
13: {
last_updated: 241,
26: {
last_updated: 363,

would be a
string of course. This object can not have more than 6 properties. When it reaches that length, I need to start replacing the oldest properties with new data. How do I get the oldest property of the object?

Answer Source

One way would be to just sort the objects keys by the last updated timestamp, and pop of the last one, the oldest one

var oldest = memory[Object.keys(memory).sort(function(a,b) {
    return memory[b].last_updated - memory[a].last_updated
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download