一个javascript关联数组长度 [英] Length of a javascript associative array

查看:145
本文介绍了一个javascript关联数组长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个js关联数组(或有些人可能preFER调用它作为一个对象)一样,说

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

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

有没有内置的功能,可以让这个数组的长度,或在jQuery的或其他库的任何解决方案?目前quesArr.length会给0,因为大多数的你一定要知道。

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.

请不要在这个问题,建议遍历整个数组/对象,因为数组/对象,我已经是非常大的。

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?

推荐答案

没有,没有内置的属性,告诉您该对象有多少属性有(这是你在找什么)。

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

我能想到的最接近的是一个只有ES5特征, Object.keys ,你可以使用这样的:

The closest I can think of is an ES5-only feature, Object.keys, which you could use like this:

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

Object.keys 返回一个对象自身的枚举的属性的名称数组。但是,对于填充工具它pre-ES5环境是非常循环你说你不想使用。 : - )

Object.keys returns an array of the names of an object's own enumerable properties. But the polyfill for it for pre-ES5 environments is the very loop you said you didn't want to use. :-)

还有另外一个功能ES5,<一个href=\"http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.4\"><$c$c>Object.getOwnPropertyNames,不管他们是否是枚举返回对象的自己的属性名称的数组。

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

FWIW,除非你打算使用阵列对象功能,不让它数组。相反:

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

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 use use an array or a plain object):

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

但是,如果你preFER可以使用其他符号;他们除了用虚线标记完全等价的密钥必须是有效的标识符(方括号中的符号,他们可以是任何东西)。

But you can use the other notation if you prefer; they're exactly equivalent except that with dotted notation the keys must be valid identifiers (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?"
};

这篇关于一个javascript关联数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆