13

I got a json object in JavaScript like:

var json = {"20121207":"13", "20121211":"9", "20121213":"7", "20121219":"4"};

without knowing the name of the last key. (The keys are in ascending order)

How can I read the value (and key) of the last element?

Penny Liu
  • 7,720
  • 5
  • 40
  • 66
Finwood
  • 3,374
  • 1
  • 14
  • 36
  • 4
    I think there is no guarantee that any of these solutions will work. Most browsers may guarantee the order of the properties, but I don't think ECMCA specs require the order to be maintained. – Nick Bray Jan 03 '13 at 22:16
  • 1
    Extensive research is available [here](http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop). – Frédéric Hamidi Jan 03 '13 at 22:24

6 Answers6

17
var highest = json[ Object.keys(json).sort().pop() ];

Object.keys (ES5, shimmable) returns an array of the object's keys. We then sort them and grab the last one.

You can't ensure order in a for..in loop, so we can't completely rely on that. But as you said the keys are in ascending order, we can simply sort them.

Marwelln
  • 25,688
  • 19
  • 82
  • 110
Zirak
  • 35,198
  • 12
  • 75
  • 89
4

Try this:

var lastKey;
var json = {"20121207":"13", "20121211":"9", "20121213":"7", "20121219":"4"};
for(var key in json){
    if(json.hasOwnProperty(key)){
        lastKey = key;
    }
}
alert(lastKey + ': ' + json[lastKey]);
Danilo Valente
  • 10,599
  • 8
  • 49
  • 65
2

If you don't need to support old JS engines:

var lastKey = Object.keys(json).sort().reverse()[0];
var lastValue = json[lastKey];

Don't assume the keys are in order. Get the keys, and then sort them, and then grab that largest value after the sort.

Alex Wayne
  • 145,435
  • 42
  • 271
  • 302
  • is this equivalent: `Object.keys(json)[json.length-1];` the keys are in ascending order, so i imagine the sort is not necessary. – Woot4Moo Jan 03 '13 at 22:16
  • What if they are not after some API change down the line? I've debugged enough stuff to be bitten by this months after the initial code was written. It's worth the sort unless performance is a major issue. – Alex Wayne Jan 03 '13 at 22:18
  • sure I understand that APIs change, just curious if it was necessary at this point in time. And yes an ounce of prevention is worth a pound of cure. – Woot4Moo Jan 03 '13 at 22:20
1

Object keys are typically unordered, but looking at the keyset you are using, I made the assumption that you are looking for the highest date in the json keys:

var json = {"20121207":"13", "20121211":"9", "20121213":"7", "20121219":"4"};

function getMaxFromKeys(json) {
    var m;
    for (var i in json) {
        if (json.hasOwnProperty(i)) {
           m = (typeof m == 'undefined' || i > m) ? i : m;
        }
    }
    return m;
}

var latestDate = getMaxFromKeys(json);
Roonaan
  • 999
  • 7
  • 10
  • This is the safest; in my case, I cannot be absolutely sure the last key is the highest (although originally designed to be so) – Chris Glasier Jun 02 '16 at 03:58
1

ECMCA script specifications (javascript specifications) do not require browsers to maintain order of the properties of an object.

Related to for-in this is what ECMCA 5 specs say:

"The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified."

Related to the Object.keys method:

"If an implementation defines a specific order of enumeration for the for-in statement, that same enumeration order must be used in step 5 of this algorithm."

It does not make sense to say get the last defined property. You can sort the properties based on the name and get the last one, but if what you want is order then keep an array.

Nick Bray
  • 1,865
  • 12
  • 18
0
let obj = <your_object>

let last_key = Object.keys(obj)[Object.keys(obj).length - 1]
let last_value = <your_object>.last_key

OR

let last_value =  Object.values(obj)[Object.values(obj).length - 1]
Keval Bhogayata
  • 1,109
  • 1
  • 6
  • 17