javascript数组_JavaScript数组

javascript数组

If JavaScript variables can be compared to the bucket on an excavator – a big, powerful, simple container that carries data in a single undifferentiated mass – then arrays are more like ice cube trays: containers that carry data in little separate “slots”.

如果可以将JavaScript变量与挖掘机上的铲斗进行比较-大型,功能强大,简单的容器以单个未区分的质量存储数据-则数组更像是冰块托盘:将数据存储在很少的单独“槽”中的容器。

Variables and arrays are not entirely distinct; arrays just a special form of variable, and should be declared as such:

变量和数组不是完全不同的。 数组只是变量的一种特殊形式,应这样声明:

var terminator_models = [];

Or more explicitly:

或更明确地说:

var terminator_models = new Array();

Both are similar, although they have subtle differences. In the examples above, they produce the same result: an empty array named terminator_models. The second option, using a Array constructor, provides the possibility of passing a length parameter to pre-determine the number of slots in the array:

两者是相似的,尽管它们之间存在细微的差异。 在上面的示例中,它们产生相同的结果:一个名为terminator_models的空数组。 第二个选项使用Array构造函数,可以通过传递length参数来预先确定数组中的插槽数:

var terminator_models = new Array(3);

For the performance-minded, it should be noted that in modern browsers creating an array with a constructor is slower than creating and sizing it directly:

对于注重性能的用户,应该注意,在现代浏览器中,使用构造函数创建数组要比直接创建数组和调整其大小慢

var terminator_models = [];
terminator_models.length = 3;

Generally speaking, it is recommended that you use var array_name = []; to create arrays.

一般来说,建议您使用var array_name = []; 创建数组。

If you already know the data that will be in the array, you can declare that on initialization:

如果您已经知道数组中的数据,则可以在初始化时声明:

var terminator_models = [ 'T-800', 'T-850', 'T-1000', 'T-1001', 'T-X' ];

An array can contain most anything: booleans, numbers, strings, functions, objects, other arrays, regular expressions, or a mixture of all of these. An example of an array that stores numbers:

数组可以包含大多数内容:布尔值,数字,字符串,函数,对象,其他数组,正则表达式或所有这些的混合。 存储数字的数组的示例:

var terminator_movies_dates = [ 1984, 1991, 2003, 2009];

索引数组 (Indexing Arrays)

By default, the first slot in an array is indexed at 0, a fact that often confuses new coders. In the console we could enter the following:

默认情况下,数组中的第一个插槽的索引为0 ,这通常会使新的编码器感到困惑。 在控制台中,我们可以输入以下内容:

var terminator_actors = [ 'Arnold Schwarzenegger', 'Robert Patrick', 
'Kristanna Loken', 'Summer Glau', 'Shirley Manson', 'Garret Dillahunt' ];
> terminator_actors[0];
"Arnold Schwarzenegger"

You can also use dot notation (i.e. terminator_actors.0) to reference an element in an array, although it comes with several conditions and exceptions that make doing so inadvisable.

您也可以使用点符号(即terminator_actors.0 )来引用数组中的元素,尽管它带有一些条件和异常,因此不建议这样做。

JavaScript does not formally support associative arrays, i.e. labelling the slots with names. However it can be fooled into such, as arrays are also objects, a possibility I’ll discuss in another article.

JavaScript正式不支持关联数组,即用名称标记插槽。 但是,由于数组也是对象 ,所以可以将其欺骗,我将在另一篇文章中讨论这种可能性。

长度 (Length)

There are many array methods, all of which I’ll cover in future articles. The most fundamental is the question of how many elements the array contains:

有很多数组方法,我将在以后的文章中介绍所有这些方法。 最基本的问题是数组包含多少个元素:

> terminator_actors.length
6

Note that length is not merely a read-only value: it can also be set. Adding new elements to the array will increase length; conversely, setting a array’s length to a number less than its population will purge entries after that point:

请注意, length不仅是一个只读值,还可以设置它。 向数组添加新元素将增加length ; 相反,将数组的length设置为小于其填充数量的数字将在此之后清除条目:

> terminator_actors;
[ 'Arnold Schwarzenegger', 'Robert Patrick', 'Kristanna Loken', 
'Summer Glau', 'Shirley Manson', 'Garret Dillahunt' ]
> terminator_actors.length = 4;
> terminator_actors;
["Arnold Schwarzenegger", "Robert Patrick", "Kristanna Loken", 
"Summer Glau"]

数组对象 (Array-Like Objects)

Perhaps the most confusing aspect of all of this is that some things in JavaScript look and act like arrays, but are not actual “true” arrays. A good example is getElementsByTagName. Let’s say you have the following page:

所有这一切中最令人困惑的方面可能是JavaScript中的某些事物在外观和行为上都类似于数组,但并不是实际的“真实”数组。 一个很好的例子是getElementsByTagName 。 假设您有以下页面:

<p>Silence. Gradually the sound of distant traffic becomes audible.
<p>A LOW ANGLE bounded on one side by a chain-link fence and on the other 
by the one-story public school buildings.
<p>Spray-can hieroglyphics and distant streetlight shadows.  This is a Los 
Angeles public school in a blue collar neighbourhood.

To which we apply this JavaScript:

我们对其应用以下JavaScript:

var p = document.getElementsByTagName("p");

For all intents and purposes, p will look and act like an array: it even has a length, and we can iterate over it. Yet some array methods won’t work on it at all:

出于所有目的和目的, p外观和行为都类似于数组:它甚至具有length ,我们可以对其进行迭代。 但是某些数组方法根本无法使用:

> p.push("test");
> TypeError: undefined is not a function

The new developer has to be careful when dealing with array-like structures: many array methods will work on them, but not all. I’ll clarify these distinctions in future articles.

新开发人员在处理类似数组的结构时必须小心:许多数组方法都可以在它们上工作,但不是全部。 我将在以后的文章中澄清这些区别。

翻译自: https://thenewcode.com/933/JavaScript-Arrays

javascript数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值