1024programmer JavaScript Array methods commonly used in JavaScript

Array methods commonly used in JavaScript

some() method

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


Output:

true

reduce () method

The array reduce () method in Javascript is used to reduce the array to a single value, and for the array Each value of (from left to right) and return value executes a provided function. Functions are stored in the accumulator.


Output:

3

map() method

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


Output:

true

flat() method

This method creates a new array that contains 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 the map function, then flattens the input array elements into a new array.


Output:

112, 52, 944  

findindex() method

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


Output:

2

find() method

This method is used to get the value of the first element in the array that satisfies the provided criteria. It checks all elements of the array, and the first element that satisfies the condition is printed.


Output:

30

fill() method

This method is used to populate the array with the given static value. The value can be used to fill the entire array, or it can be used to fill a portion of the array.


Output:

1, 87, 87  ,58

forEach() method

This method calls the provided function once for each element of the array. Provided functions can perform any type of operation on the elements of the given array.


Output:

10, 25, 50  、88

concat() method

This method is used to merge two or more arrays together . This function does not alter the original array passed as an argument.


Output:

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

include() method

This method It is used to know if a particular element is present in the array, therefore, it returns true or false, i.e., if the element exists, it returns true, otherwise it returns false.


Output:

true

reverse() method

This method is used for in-place inversion of an array. The first element of the array becomes the last element and vice versa.

 function func() {
 //Original Array
 var arr = [34, 234, 567, 4];
 document.write("Original array: " + arr);
 //Reversed array
 var new_arr = arr. reverse();
 document.write("
Newly reversed array: "); document.write(new_arr); } func();

Output:

Original array: 34, 234, 567, 4 New reversed array: 4, 567, 234, 34

Recommended tutorial: “JS Tutorial”

The above is the detailed content of array methods commonly used in Javascript. For more, please pay attention to other related articles on 1024programmer.com!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/array-methods-commonly-used-in-javascript/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索