本文共 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 ]
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)
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()
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]
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/