Javascript 在javascript中获取json对象的最后一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14148065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:50:42  来源:igfitidea点击:

get last element of a json object in javascript

javascriptjson

提问by Finwood

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)

我在 javascript 中得到了一个 json 对象,就像
var json = {"20121207":"13", "20121211":"9", "20121213":"7", "20121219":"4"};
不知道最后一个键的名称一样。(键按升序排列)

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

如何读取最后一个元素的值(和键)?

回答by Zirak

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.

Object.keys(ES5, shimmable) 返回对象键的数组。然后我们对它们进行排序并抓住最后一个。

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

您无法确保for..in循环中的顺序,因此我们不能完全依赖它。但是正如你所说的键是按升序排列的,我们可以简单地对它们进行排序。

回答by Danilo Valente

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]);

回答by Alex Wayne

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

如果你不需要支持旧的 JS 引擎:

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.

不要假设键是有序的。获取键,然后对它们进行排序,然后在排序后获取最大值。

回答by Roonaan

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:

对象键通常是无序的,但是查看您正在使用的键集,我假设您正在寻找 json 键中的最高日期:

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);

回答by Nick Bray

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

EMCCA 脚本规范(javascript 规范)不要求浏览器维护对象属性的顺序。

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

与 for-in 相关的是 EMCCA 5 规范所说的:

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

“枚举属性的机制和顺序(第一个算法中的步骤 6.a,第二个算法中的步骤 7.a)没有指定。”

Related to the Object.keys method:

与 Object.keys 方法相关:

"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."

“如果实现为 for-in 语句定义了特定的枚举顺序,则必须在此算法的第 5 步中使用相同的枚举顺序。”

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.

说获取最后定义的属性是没有意义的。您可以根据名称对属性进行排序并获得最后一个,但如果您想要的是顺序,则保留一个数组。

回答by Keval Bhogayata

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]