get last element of a json object in javascript

后端 未结 6 1009
谎友^
谎友^ 2020-12-15 08:58

I got a json object in javascript like
var json = {\"20121207\":\"13\", \"20121211\":\"9\", \"20121213\":\"7\", \"20121219\":\"4\"};
without knowing the

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-15 09:44

    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.

提交回复
热议问题