get last element of a json object in javascript

后端 未结 6 1039
谎友^
谎友^ 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:45

    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.

提交回复
热议问题