javascript 关联数组的长度

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

Length of a javascript associative array

javascriptarraysjavascript-objects

提问by gopi1410

I have a js associative array (or some may prefer to call it as an object)like, say

我有一个 js 关联数组(或者有些人可能更喜欢将其称为对象),例如

var quesArr=new Array();
quesArr["q101"]="Your name?";
quesArr["q102"]="Your age?";
quesArr["q103"]="Your school?";

Is there any built in function that could get the length of this array, or any solution in jQuery or other library? Currently quesArr.length would give 0, as most of you must be knowing.

是否有任何内置函数可以获取此数组的长度,或者 jQuery 或其他库中的任何解决方案?目前 quesArr.length 会给出 0,因为你们大多数人都知道。

Please dont suggest iterating over the entire array/object as mentioned in this question, because the array/object which I have is very large.

请不要建议像这个问题中提到的那样迭代整个数组/对象,因为我拥有的数组/对象非常大。

Is there any way I could proceed with this?

有什么办法可以继续吗?

回答by T.J. Crowder

No, there is no built-in property that tells you how many properties the object has (which is what you're looking for).

不,没有内置属性可以告诉您对象具有多少属性(这就是您要查找的属性)。

The closest I can think of is an ES5 and higher feature, Object.keys(spec| MDN), which you could use like this:

我能想到的最接近的是 ES5 和更高的功能,Object.keysspec| MDN),你可以这样使用:

console.log(Object.keys(quesArr).length); // "3"

Object.keysreturns an array of the names of an object's own enumerable properties. But internally of course it's that loop you didn't want to use (and the polyfill for it for pre-ES5 environments uses a loop, of course).

Object.keys返回对象自身可枚举属性名称的数组。但在内部当然是你不想使用的循环(当然,在 ES5 之前的环境中,它的 polyfill 使用了循环)。

There's also another ES5+ feature, Object.getOwnPropertyNames(spec| MDN), which returns an array of the object's "own" property names regardless of whether they're enumerable.

还有另一个 ES5+ 特性Object.getOwnPropertyNamesspec| MDN),它返回对象“自己的”属性名称的数组,而不管它们是否可枚举。

Both are supported in anything vaguely modern. IE9+, and versions of Chrome, Firefox, and Safari since at least 2011.

两者都支持任何模糊的现代。IE9+ 以及至少 2011 年以来的 Chrome、Firefox 和 Safari 版本。



FWIW, unless you're going to use the Arrayfeatures of the object, don't make it an array. Instead:

FWIW,除非您要使用Array对象的功能,否则不要将其设为数组。反而:

var quesArr = {};
quesArr["q101"] = "Your name?";
quesArr["q102"] = "Your age?";
quesArr["q103"] = "Your school?";

Those keys don't have to be given as string literals in square brackets, either, if you don't want them to be (whether you use an array or a plain object):

这些键也不必作为方括号中的字符串文字给出,如果您不希望它们是(无论您使用数组还是普通对象):

var quesArr = {};
quesArr.q101 = "Your name?";
quesArr.q102 = "Your age?";
quesArr.q103 = "Your school?";

But you can use the other notation if you prefer; they're exactly equivalent except that with dotted notation the keys must be valid identifier names (in bracketed notation they can be anything).

但如果您愿意,也可以使用其他表示法;它们完全等效,只是使用点符号表示键必须是有效的标识符名称(在括号中,它们可以是任何东西)。

You can even do this:

你甚至可以这样做:

var quesArr = {
    q101: "Your name?",
    q102: "Your age?",
    q103: "Your school?"
};

or (if the keys won't be valid identifiers):

或(如果密钥不是有效标识符):

var quesArr = {
    "q101": "Your name?",
    "q102": "Your age?",
    "q103": "Your school?"
};

Those can be single or double quotes.

这些可以是单引号或双引号。

回答by kmario23

Suppose you have the following,

假设你有以下内容,

var myObject = {};  // denotes an Object is being created  
myObject.city = "Chennai";  
myObject.state = "Tamilnadu";  
myObject.country = "Indian Peninsula";  
console.log(myObject.length);  // results in undefined  

But, there is a way to calculate the length in modern browsers.(Chrome, FF4+, IE9)

但是,有一种方法可以在现代浏览器中计算长度。(Chrome、FF4+、IE9)

Object.keys(myObject);  
// --> ["city", "state", "country"]
Object.keys(myObject).length
// --> 3

回答by user2256383

One can also deal with the issue of associative arrays length matters by keeping track of the length of the array with a associative element called length.

还可以通过使用称为 length 的关联元素跟踪数组的长度来处理关联数组长度问题。

aka:

又名

myArray["length"]=0;

As you add or remove elements simply add or subtract from the myArrary["length"]element.

添加或删除元素时,只需从元素中添加或减去即可myArrary["length"]

Then you can do things such as:

然后,您可以执行以下操作:

theLength=myArray.length;

回答by viveksharma

Associative array does not have length property you can get length

关联数组没有长度属性 你可以得到长度