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>
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.
<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>
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.
<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)
Output:
2 3 4 5
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>
Output:
true
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>
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.
<script>const myAwesomeArray = [[1], [2], [3], [4], [5]]var geeks = myAwesomeArray.flatMap(arr => arr * 10) console.log(geeks);
Output:
10、20、30、40、50
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>
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.
<script> var array = [ 10, 20, 30, 110, 60 ]; function finding_index(element) { return element > 25; } document.write(array.findIndex(finding_index)); </script>
Output:
2
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>
输出:
30
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>
输出:
1,87,87,58
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>
输出:
1,841,2209
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>
输出:
10、25、50、88
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>
输出:
11,12,13,14,15,15,16,17,18,19
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>
输出:
true
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>
输出:
原始数组:34、234、567、4新反向阵列:4、567、234、34
推荐教程:《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!