Array.prototype 常用方法整理

分类归纳整理常用的 Array 内置方法,加深 Array 内置方法的理解和记忆。本文不包含试验性属性、非标准属性或被放弃的属性。

会改变自身的方法

下面的这些方法会改变调用它们的对象自身的值:

Array.prototype.copyWithin()

Copies a sequence of array elements within the array.

  • Syntax

    arr.copyWithin(target[, start[, end]])

Array.prototype.fill()

Fills all the elements of an array from a start index to an end index with a static value.

  • Syntax

    arr.fill(value[, start[, end]])

Array.prototype.pop()

Removes the last element from an array and returns that element.

Array.prototype.push()

Adds one or more elements to the end of an array and returns the new length of the array.

let arr = [1, 2, 3]
let newArr = [...arr, 4, 5]

Array.prototype.reverse()

Reverses the order of the elements of an array in place — the first becomes the last, and the last becomes the first.

Array.prototype.shift()

Removes the first element from an array and returns that element.

Array.prototype.sort()

Sorts the elements of an array in place and returns the array.

Array.prototype.splice()

Adds and/or removes elements from an array.

Array.prototype.unshift()

Adds one or more elements to the front of an array and returns the new length of the array.

let arr = [1, 2, 3]
let newArr = [-1, 0, ...arr]

不会改变自身的方法

下面的这些方法绝对不会改变调用它们的对象的值,只会返回一个新的数组或者返回一个其它的期望值。

Array.prototype.concat()

Returns a new array comprised of this array joined with other array(s) and/or value(s).

Array.prototype.includes()

Determines whether an array contains a certain element, returning true or false as appropriate.

Array.prototype.indexOf()

Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.

Array.prototype.join()

Joins all elements of an array into a string.

Array.prototype.lastIndexOf()

Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.

Array.prototype.slice()

Extracts a section of an array and returns a new array.

  • Syntax

    arr.slice([begin[, end]])

Array.prototype.toString()

Returns a string representing the array and its elements. Overrides the Object.prototype.toString() method.

Array.prototype.toLocaleString()

Returns a localized string representing the array and its elements. Overrides the Object.prototype.toLocaleString() method.

遍历方法

在下面的众多遍历方法中,有很多方法都需要指定一个回调函数作为参数。

Array.prototype.entries()

Returns a new Array Iterator object that contains the key/value pairs for each index in the array.

Array.prototype.every()

Returns true if every element in this array satisfies the provided testing function.

Array.prototype.filter()

Creates a new array with all of the elements of this array for which the provided filtering function returns true.

Array.prototype.find()

Returns the found value in the array, if an element in the array satisfies the provided testing function or undefined if not found.

  • Syntax

    arr.find(callback[, thisArg])

  • Return value

    The value of the first element in the array that satisfies the provided testing function; otherwise, undefined is returned.

Array.prototype.findIndex()

Returns the found index in the array, if an element in the array satisfies the provided testing function or -1 if not found.

Array.prototype.forEach()

Calls a function for each element in the array.

Array.prototype.keys()

Returns a new Array Iterator that contains the keys for each index in the array.

Array.prototype.map()

Creates a new array with the results of calling a provided function on every element in this array.

Array.prototype.reduce()

Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value.

  • Example

    const flatten = function(arr) {
    return arr.reduce((flat, cur) => {
    return flat.concat(Array.isArray(cur) ? flatten(cur) : cur)
    }, [])
    }

    let arr = [1, 2, [3, 4, [5, 6]]]

    console.log(flatten(arr)) // [ 1, 2, 3, 4, 5, 6 ]

Array.prototype.reduceRight()

Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value.

Array.prototype.some()

Returns true if at least one element in this array satisfies the provided testing function.

Array.prototype.values()

Returns a new Array Iterator object that contains the values for each index in the array.

Array.prototype@@iterator

Returns a new Array Iterator object that contains the values for each index in the array.

加载评论框需要翻墙