Home>Article>Web Front-end> Commonly used array methods in JavaScript

Commonly used array methods in JavaScript

hzc
hzc forward
2020-06-04 13:26:48 2574browse

some() method

This method checks whether at least one element of the array meets the conditions checked by the parameter function.

Output:

true

reduce() method

The array reduce() method in JavaScript is used to reduce an array into a single value and for each value of the array (from left to right) and the return value executes a provided function. The function is stored in the accumulator.

Output:

3

map() method

The map() method in JavaScript creates an array by calling a specific function on each element present in the parent array . This is a non-mutating method. Typically, the map() method is used to iterate over an array and call a function on each element of the array.

Output:

true

flat() method

This method creates a new array containing multiple arrays. Basically create a simple array from an array containing multiple arrays.

Output:

11,89,23,7,98

flatMap() method

This method is used to flatten the input array elements into a new array. This method first maps each element with the help of map function and then flattens the input array elements into a new array.

Output:

112、52、944

findindex() method

This method returns the index of the first element in the given array that satisfies the provided test function. Otherwise, -1 is returned.

Output:

2

find()方法

此方法用于获取满足所提供条件的数组中第一个元素的值。它检查数组的所有元素,以及第一个满足条件的要打印的元素。

输出:

30

fill()方法

此方法用于使用给定的静态值填充数组。该值可以用于填充整个数组,也可以用于填充数组的一部分。

输出:

1,87,87,58

forEach()方法

该方法为数组的每个元素调用一次提供的函数。提供的函数可以对给定数组的元素执行任何类型的操作。

输出:

10、25、50、88

concat()方法

此方法用于将两个或多个数组合并在一起。此函数不会更改作为参数传递的原始数组。

输出:

11,12,13,14,15,15,16,17,18,19

include()方法

此方法用于知道数组中是否存在特定元素,因此,它返回true或false,即,如果该元素存在,则返回true,否则返回false。

输出:

true

reverse()方法

此方法用于数组的就地反转。数组的第一个元素变为最后一个元素,反之亦然。