博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript数组简介
阅读量:2504 次
发布时间:2019-05-11

本文共 3595 字,大约阅读时间需要 11 分钟。

An array is a collection of elements.

数组是元素的集合。

Arrays in JavaScript are not a type on their own.

JavaScript中的数组本身不是一种类型

Arrays are objects.

数组是对象

We can initialize an empty array in these 2 different ways:

我们可以通过以下两种不同的方式初始化一个空数组:

const a = []const a = Array()

The first is using the array literal syntax. The second uses the Array built-in function.

第一种是使用数组文字语法 。 第二个使用Array内置函数。

You can pre-fill the array using this syntax:

您可以使用以下语法预先填充数组:

const a = [1, 2, 3]const a = Array.of(1, 2, 3)

An array can hold any value, even value of different types:

数组可以包含任何值,甚至可以包含不同类型的值:

const a = [1, 'Flavio', ['a', 'b']]

Since we can add an array into an array, we can create multi-dimensional arrays, which have very useful applications (e.g. a matrix):

由于我们可以将数组添加到数组中,因此我们可以创建多维数组,这些数组具有非常有用的应用程序(例如矩阵):

const matrix = [  [1, 2, 3],  [4, 5, 6],  [7, 8, 9]]matrix[0][0] //1matrix[2][0] //7

You can access any element of the array by referencing its index, which starts from zero:

您可以通过引用从零开始的索引来访问数组的任何元素:

a[0] //1a[1] //2a[2] //3

You can initialize a new array with a set of values using this syntax, which first initializes an array of 12 elements, and fills each element with the 0 number:

您可以使用以下语法用一组值初始化一个新数组,该语法首先初始化一个包含12个元素的数组,然后用0填充每个元素:

Array(12).fill(0)

You can get the number of elements in the array by checking its length property:

您可以通过检查其length属性来获取数组中元素的数量:

const a = [1, 2, 3]a.length //3

Note that you can set the length of the array. If you assign a bigger number than the arrays current capacity, nothing happens. If you assign a smaller number, the array is cut at that position:

请注意,您可以设置数组的长度。 如果分配的数字大于阵列的当前容量,则不会发生任何事情。 如果分配较小的数字,则会在该位置切割数组:

const a = [1, 2, 3]a //[ 1, 2, 3 ]a.length = 2a //[ 1, 2 ]

如何将项目添加到数组 (How to add an item to an array)

We can add an element at the end of an array using the push() method:

我们可以使用push()方法在数组的末尾添加一个元素:

a.push(4)

We can add an element at the beginning of an array using the unshift() method:

我们可以使用unshift()方法在数组的开头添加一个元素:

a.unshift(0)a.unshift(-2, -1)

如何从数组中删除项目 (How to remove an item from an array)

We can remove an item from the end of an array using the pop() method:

我们可以使用pop()方法从数组末尾删除一个项目:

a.pop()

We can remove an item from the beginning of an array using the shift() method:

我们可以使用shift()方法从数组的开头删除一项:

a.shift()

如何加入两个或多个数组 (How to join two or more arrays)

You can join multiple arrays by using concat():

您可以使用concat()连接多个数组:

const a = [1, 2]const b = [3, 4]const c = a.concat(b) //[1,2,3,4]a //[1,2]b //[3,4]

You can also use the spread operator (...) in this way:

您还可以通过以下方式使用扩展运算符( ... ):

const a = [1, 2]const b = [3, 4]const c = [...a, ...b]c //[1,2,3,4]

如何在数组中查找特定项目 (How to find a specific item in the array)

You can use the find() method of an array:

您可以使用数组的find()方法:

a.find((element, index, array) => {  //return true or false})

Returns the first item that returns true. Returns undefined if the element is not found.

返回第一个返回true的项目。 如果找不到该元素,则返回undefined。

A commonly used syntax is:

常用的语法是:

a.find(x => x.id === my_id)

The above line will return the first element in the array that has id === my_id.

上一行将返回数组中具有id === my_id的第一个元素。

findIndex() works similarly to find(), but returns the index of the first item that returns true, and if not found, it returns -1:

findIndex()find()类似,但返回返回true的第一项的索引,如果找不到,则返回-1

a.findIndex((element, index, array) => {  //return true or false})

Another method is includes():

另一种方法是includes()

a.includes(value)

Returns true if a contains value.

返回true如果a包含value

a.includes(value, i)

Returns true if a contains value after the position i.

如果a在位置i之后包含value ,则返回true。

翻译自:

转载地址:http://zlqgb.baihongyu.com/

你可能感兴趣的文章
JavaScript高级特性-实现继承的七种方式
查看>>
20121016学习笔记四
查看>>
EntityFramework 学习 一 Stored Procedure
查看>>
Sliverlight之 故事板
查看>>
Java 必知必会的 20 种常用类库和 API
查看>>
HDU 1087 Super Jumping! Jumping! Jumping!
查看>>
0007_初始模块和字节码
查看>>
[效率提升]如何管理好你的电脑文件
查看>>
C++实验二
查看>>
Sultan's Dowry Problem - 苏丹新娘问题
查看>>
SharePoint2010 富文本框添加图片功能的扩展
查看>>
零零碎碎的知识
查看>>
UNIX基础--用户和基本账户管理
查看>>
设计模式
查看>>
5.0以上机器XPOSED框架安装流程
查看>>
使用spring aop 记录接口日志
查看>>
静态方法与非静态方法
查看>>
[转]iOS进阶路线以及进阶书籍
查看>>
@RequestParam、@RequestBody和@ModelAttribute区别
查看>>
Codeforces Round #413 B T-shirt buying (STL set)
查看>>