Javascript 如何在javascript中获取数组中的最后一个键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2199060/
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-22 23:12:15  来源:igfitidea点击:

How to get last key in array in javascript?

javascriptarrays

提问by user198729

This is similar to this question,only PHP->javascript

这类似于这个问题,只有 PHP->javascript

How to get numeric key of new pushed item in PHP?

如何在PHP中获取新推送项目的数字键?

回答by Matthew Flaschen

var foo = myarray[myarray.length - 1];

The preferred term is element, rather than key.

首选术语是元素,而不是键。

EDIT: Or do you mean the last index? That is myarray.length - 1. Keep in mind, JavaScript arrays can only be indexed numerically (though arrays are also objects, which causes some confusion).

编辑:或者你的意思是最后一个索引?那就是myarray.length - 1。请记住,JavaScript 数组只能用数字索引(尽管数组也是对象,这会引起一些混淆)。

回答by Aron Rotteveel

If it's a flat array, this would do:

如果它是一个flat array,这会做:

return array.length - 1;

However, if you have an associative array, you'd have to know the key and loop through it. Please note though that JavaScript knows no such thing as an "associative array", as most elements in JavaScript are objects.

但是,如果您有一个关联数组,则必须知道键并循环遍历它。请注意,尽管 JavaScript 不知道“关联数组”这样的东西,因为 JavaScript 中的大多数元素都是对象。

Disclaimer:Usage of associative arrays in JavaScript is generally not a good practiceand can lead to problems.

免责声明:在 JavaScript 中使用关联数组通常不是一个好习惯,可能会导致问题。

var x = new Array();
x['key'] = "value";
for (i in x)
{
    if (i == 'key')
    {
        alert ("we got "+i);
    }
}

回答by persian

first sorry for poor English. assume your array is "key associated array" with string keys in number format . you will need 3 step : get all array keys . convert array keys to integer. get max or length or any other keys property.

首先抱歉英语不好。假设您的数组是带有数字格式的字符串键的“键关联数组”。您将需要 3 个步骤:获取所有数组键。将数组键转换为整数。获取 max 或 length 或任何其他键属性。

stringKeys= Object.keys(keyValueArray);//get keys of array 
integerKeys=stringKeys.map(Number);//convert to integer
mx=Math.max.apply(integerKeys);//get max
len=stringKeys.length;//get length 

hope i helped

希望我有所帮助

回答by Gumbo

The last key of an array is always arr.length-1as arrays always start with key 0:

数组的最后一个键总是arr.length-1因为数组总是以 key 开头0

var arr = new Array(3);  // arr === [], arr.length === 3
arr.push(0);             // arr === [undefined, undefined, undefined, 0], arr.length === 4
arr[arr.length-1]        // returns 0

var arr = [];            // arr === [], arr.length === 0
arr[3] = 0;              // arr === [undefined, undefined, undefined, 0], arr.length === 4
arr[arr.length-1]        // returns 0

回答by ssten

If your array is an associative array(Object), I think the best way to do this is by using Object.keys().

如果您的数组是关联数组(Object),我认为最好的方法是使用Object.keys().

From MDN;

来自 MDN

The Object.keys() method returns an array of a given object's own property names, in the same order as we get with a normal loop.

Object.keys() 方法返回给定对象自己的属性名称的数组,其顺序与我们使用普通循环获得的顺序相同。

First get the keys of the array in an numeric array.
Then get the last key, and use it in the Object.

首先在数值数组中获取数组的键。
然后得到最后一个key,在Object中使用。

var keys = Object.keys(my_array);
var last = keys[keys.length - 1];
console.log(my_array[last]);

回答by Mamun Morshed

Math.max(...[...myArray.keys()]);

Here myArrayis the name of an array.

myArray是数组的名称。

This is a solution based on your referred question. Assuming the array is a single dimensional array or all the array keys are numeric.

这是基于您提到的问题的解决方案。假设数组是一维数组或所有数组键都是数字。

Reads: Math.max, Array.prototype.keys(), Iterators and generators

读取: Math.maxArray.prototype.keys()迭代器和生成器