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

Commonly used array methods in JavaScript

hzc
Release: 2020-06-04 13:35:28
forward
2727 people have browsed it

some() method

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

<script>
// JavaScript to illustrate 
// lastIndexOf() method 
function isGreaterThan5(element, index, array) {  
    return element > 5;  
}
function func() {
    // Original array  
    var array = [2, 5, 8, 1, 4];
    // Checking for condition in array  
    var value = array.some(isGreaterThan5);
    document.write(value);  
}
func();  
</script>
Copy after login

Output:

true
Copy after login
Copy after login
Copy after login

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.

<script>
// Original array 
var numbers = [88, 50, 25, 10];
// Performing reduce method 
var sub = numbers.reduce(geeks);
function geeks(total, num) { 
    return total - num; 
}
document.write(sub) 
</script>
Copy after login

Output:

3
Copy after login

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.

<script> // Original array 
var numbers = [4, 9, 16, 25]; 
// Performing map method 
var sub = numbers.map(geeks); 
function geeks() {     
return numbers.map(Math.sqrt); 
} 
document.write(sub)
Copy after login

Output:

2 3 4 5
Copy after login

every() method

This method checks whether all elements of the array satisfy the given condition, which is provided by the function passed to it as a parameter .

<script>  // JavaScript code for every() function  
function ispositive(element, index, array) {      
return element > 0;  }  function func() {      
var arr = [ 11, 89, 23, 7, 98 ];      
// Check for positive number      
var value = arr.every(ispositive);      
document.write(value);  }  
func();  
</script>
Copy after login

Output:

true
Copy after login
Copy after login
Copy after login

flat() method

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

<script>
//Original array 
var arr = [ [11, 89], [23, 7], 98 ];
// Performing flat method 
var geeks = arr.flat();
document.write(geeks) 
</script>
Copy after login

Output:

11,89,23,7,98
Copy after login

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.

<script>const myAwesomeArray = [[1], [2], [3], [4], [5]]var geeks = myAwesomeArray.flatMap(arr => arr * 10) console.log(geeks);
Copy after login

Output:

10、20、30、40、50
Copy after login

filter() method

This method is used to create a new array from the given array consisting only of the parameters in the given array that satisfy the function Consists of those elements that set conditions.

<script>
function isPositive(value) {  
    return value > 0;  
}
function func() {  
    var filtered = [112, 52, 0, -1, 944] 
    .filter(isPositive);  
    document.write(filtered);  
}
func();  
</script>
Copy after login

Output:

112、52、944
Copy after login

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.

<script>
var array = [ 10, 20, 30, 110, 60 ];
function finding_index(element) {  
    return element > 25; 
}
document.write(array.findIndex(finding_index));  
</script>
Copy after login

Output:

2
Copy after login

find()方法

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

<script>
// Input array contain some elements.  
var array = [10, 20, 30, 40, 50];
// Function (return element > 10).  
var found = array.find(function(element) {  
    return element > 20;  
});
// Printing desired values.  
document.write(found);  
</script>
Copy after login

输出:

30
Copy after login

fill()方法

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

<script>
// JavaScript code for fill() function  
function func() {
    var arr = [1, 23, 46, 58];
    // Here value = 87, start index = 1 and  
    // and last index = 3  
    arr.fill(87, 1, 3);  
    document.write(arr);  
}
func();  
</script>
Copy after login

输出:

1,87,87,58
Copy after login

forEach()方法

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

<script>  function func() {      
// Original array      
const items = [1, 29, 47];      
const copy = [];      
items.forEach(function(item){          
copy.push(item*item);      });      
document.write(copy);  }  
func();  
<script>
Copy after login

输出:

1,841,2209
Copy after login

sort()方法

此方法用于对数组进行排序。数组可以是任何类型,例如字符串,数字,字符等。

<script>
// Original array 
var numbers = [88, 50, 25, 10];
// Performing sort method 
var sub = numbers.sort(geeks);
function geeks(a, b) { 
    return a - b; 
}
document.write(sub) 
</script>
Copy after login

输出:

10、25、50、88
Copy after login

concat()方法

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

<script>
// JavaScript code for concat() function  
function func() {  
    var num1 = [11, 12, 13],  
        num2 = [14, 15, 16],  
        num3 = [17, 18, 19];
    document.write(num1.concat(num2, num3));  
}  
func();  
</script>
Copy after login

输出:

11,12,13,14,15,15,16,17,18,19
Copy after login

include()方法

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

<script>
    // Taking input as an array A  
    // having some elements.  
    var A = [ 1, 2, 3, 4, 5 ];
    // Include() function is called to  
    // test whether the searching element  
    // is present in given array or not.  
    a = A.includes(2)
    // Printing result of function.  
    document.write(a);  
</script>
Copy after login

输出:

true
Copy after login
Copy after login
Copy after login

reverse()方法

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

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

输出:

原始数组:34、234、567、4新反向阵列:4、567、234、34
Copy after login

推荐教程:《JS教程

The above is the detailed content of Commonly used array methods in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template