JavaScript 关联数组的长度 [英] Length of a JavaScript associative array

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

问题描述

我有一个 JavaScript 关联数组(或者有些人可能更喜欢称它为对象),比如

I have a JavaScript associative array (or some may prefer to call it 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 a built-in function that could get the length of this array, or a solution in jQuery or another library? Currently quesArr.length would give 0, as most of you must be knowing.

请不要建议像 this 中提到的那样遍历整个数组/对象问题,因为我拥有的数组/对象非常大.

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

有没有办法让我继续这样做?

Is there a 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 (规范 | MDN) 和 Object.getOwnPropertyNames(spec | MDN).例如,您可以像这样使用 Object.keys:

The closest I can think of are two ES5 and higher features, Object.keys (spec | MDN) and Object.getOwnPropertyNames (spec | MDN). For instance, you could use Object.keys like this:

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

Object.keys 返回对象自己的可枚举字符串命名属性的名称数组.但是在内部(理论上)它是您不想使用的循环(当然,用于 ES5 之前环境的 polyfill 使用循环).如果您还需要不可枚举的字符串命名属性,则可以改用 Object.getOwnPropertyNames.

Object.keys returns an array of the names of an object's own enumerable string-named properties. But internally (in theory) it's that loop you didn't want to use (and the polyfill for it for pre-ES5 environments uses a loop, of course). If you also want non-enumerable string-named properties, you'd use Object.getOwnPropertyNames instead.

在 ES2015+ 中,对象可以具有键为 Symbols 而不是字符串的属性.Object.getOwnPropertySymbols(spec | MDN) 让你得到它们.

In ES2015+, an object can have properties whose keys are Symbols rather than strings. Object.getOwnPropertySymbols (spec | MDN) lets you get them.

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

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

你甚至可以这样做:

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.

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

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