Array
15 methods值的有序列表,从零开始索引,大小动态可变。
arr.map(callback, thisArg?)返回一个新数组,其中每个元素都是对原元素调用 callback 的结果。
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => any | 生成新元素的函数。 |
| thisArg | any | 在 callback 中用作 `this` 的值。 |
Returns
Array
Example
javascript
[1, 2, 3].map(x => x * 2) // [2, 4, 6]
['a', 'b'].map((v, i) => `${i}:${v}`) // ['0:a', '1:b']arr.filter(callback, thisArg?)返回一个新数组,包含所有使 callback 返回真值的元素。
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => boolean | 谓词函数。 |
| thisArg | any | 在 callback 中用作 `this` 的值。 |
Returns
Array
Example
javascript
[1, 2, 3, 4].filter(x => x % 2 === 0) // [2, 4]
['', 'a', null].filter(Boolean) // ['a']arr.reduce(callback, initialValue?)对累加器和每个元素应用 callback,归约为单个值。
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (acc, value, index, array) => acc | 归约函数。 |
| initialValue | any | 累加器的初始值。 |
Returns
any
Example
javascript
[1, 2, 3, 4].reduce((a, b) => a + b, 0) // 10
['a', 'b', 'c'].reduce((acc, v) => ({...acc, [v]: true}), {}) // {a:true, b:true, c:true}arr.forEach(callback, thisArg?)对每个元素执行一次 callback。返回 undefined。
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => void | 对每个元素执行的函数。 |
| thisArg | any | 在 callback 中用作 `this` 的值。 |
Returns
undefined
Example
javascript
[1, 2, 3].forEach(x => console.log(x)) // logs 1, 2, 3arr.find(callback, thisArg?)返回第一个使 callback 返回真值的元素,否则返回 undefined。
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => boolean | 谓词函数。 |
| thisArg | any | 在 callback 中用作 `this` 的值。 |
Returns
any
Example
javascript
[1, 2, 3, 4].find(x => x > 2) // 3
[{id: 1}, {id: 2}].find(o => o.id === 2) // {id: 2}arr.some(callback, thisArg?)如果至少有一个元素满足谓词则返回 true。
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => boolean | 谓词函数。 |
| thisArg | any | 在 callback 中用作 `this` 的值。 |
Returns
boolean
Example
javascript
[1, 2, 3].some(x => x > 2) // true
[1, 2, 3].some(x => x > 5) // falsearr.every(callback, thisArg?)如果所有元素都满足谓词则返回 true。
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => boolean | 谓词函数。 |
| thisArg | any | 在 callback 中用作 `this` 的值。 |
Returns
boolean
Example
javascript
[2, 4, 6].every(x => x % 2 === 0) // true
[2, 4, 5].every(x => x % 2 === 0) // falsearr.includes(searchElement, fromIndex?)如果数组包含 searchElement 则返回 true。
Parameters
| Name | Type | Description |
|---|---|---|
| searchElement | any | 要查找的值。 |
| fromIndex | number | 起始索引(默认 0)。 |
Returns
boolean
Example
javascript
[1, 2, 3].includes(2) // true
[1, 2, 3].includes(2, 2) // false
['a', 'b'].includes('a') // truearr.indexOf(searchElement, fromIndex?)返回 searchElement 首次出现的索引,未找到则返回 -1。
Parameters
| Name | Type | Description |
|---|---|---|
| searchElement | any | 要查找的值。 |
| fromIndex | number | 起始索引(默认 0)。 |
Returns
number
Example
javascript
[1, 2, 1].indexOf(1) // 0
[1, 2, 1].indexOf(1, 1) // 2
[1, 2].indexOf(3) // -1arr.slice(start?, end?)返回从 start 到 end(不包含 end)的数组部分的浅拷贝。
Parameters
| Name | Type | Description |
|---|---|---|
| start | number | 起始索引(默认 0)。 |
| end | number | 结束索引(默认为 length)。 |
Returns
Array
Example
javascript
[1, 2, 3, 4].slice(1, 3) // [2, 3]
[1, 2, 3, 4].slice(-2) // [3, 4]
[1, 2, 3].slice() // [1, 2, 3] (shallow copy)arr.splice(start, deleteCount?, ...items)就地移除、替换或插入元素。返回被移除的元素。
Parameters
| Name | Type | Description |
|---|---|---|
| start | number | 起始索引。 |
| deleteCount | number | 要移除的元素数量。 |
| items | any | 要在 start 处插入的元素。 |
Returns
Array
Example
javascript
let a = [1, 2, 3];
a.splice(1, 1) // [2], a == [1, 3]
a.splice(1, 0, 'x') // [], a == [1, 'x', 3]arr.concat(...values)返回一个新数组,为该数组与 values 连接后的结果。
Parameters
| Name | Type | Description |
|---|---|---|
| values | ...any | 要连接的数组或值。 |
Returns
Array
Example
javascript
[1, 2].concat([3, 4]) // [1, 2, 3, 4]
[1, 2].concat(3, [4]) // [1, 2, 3, 4]arr.sort(compareFn?)就地排序数组。未提供 compareFn 时,元素按 UTF-16 码单元以字符串方式排序。
Parameters
| Name | Type | Description |
|---|---|---|
| compareFn | (a, b) => number | 返回负数、0 或正数的比较函数。 |
Returns
Array
Example
javascript
[3, 1, 2].sort() // [1, 2, 3]
[3, 1, 2].sort((a, b) => b - a) // [3, 2, 1]
[10, 1, 2].sort() // [1, 10, 2] (string sort!)arr.reverse()就地反转数组。
Returns
Array
Example
javascript
[1, 2, 3].reverse() // [3, 2, 1]arr.flat(depth?)返回一个新数组,将子数组连接至 depth 层(默认 1)。
Parameters
| Name | Type | Description |
|---|---|---|
| depth | number | 要展平的嵌套深度(默认 1)。 |
Returns
Array
Example
javascript
[1, [2, [3, [4]]]].flat() // [1, 2, [3, [4]]]
[1, [2, [3, [4]]]].flat(2) // [1, 2, 3, [4]]
[1, [2, [3, [4]]]].flat(Infinity) // [1, 2, 3, 4]