Some () method

This method checks that at least one element of the array meets the criteria for the parameter function check.

<script>
// JavaScript to illustrate 
// lastIndexOf() method 
function isGreaterThan5(element, index, array) {  
    return element > 5;  
}
function func(a) {
    // 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 the code

Output:

true
Copy the code

Reduce () method

The array reduce () method in JavaScript is used to reduce an array to a single value and to perform a provided function for each value (left to right) and return value of the array. Functions are 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 the code

Output:

3
Copy the code

Map () method

The map () method in JavaScript creates an array by calling a specific function on each element that exists in the parent array. This is a non-mutational approach. In general, the map () method is used to iterate through an array of numbers 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(a) {     
return numbers.map(Math.sqrt); 
} 
document.write(sub)
Copy the code

Output:

2 3 4 5
Copy the code

Every () method

This method checks whether all the elements of the array satisfy a given condition provided by the function passed to it as an argument.

<script>  // JavaScript code for every() function  
function ispositive(element, index, array) {      
return element > 0;  }  function func(a) {      
var arr = [ 11.89.23.7.98 ];      
// Check for positive number      
var value = arr.every(ispositive);      
document.write(value);  }  
func();  
</script> 
Copy the code

Output:

true
Copy the code

Flat () method

This method creates a new array that contains 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 the code

Output:

11.89.23.7.98
Copy the code

FlatMap () method

This method is used to flatten the elements of the input array into a new array. This method first maps each element with a mapping 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 the code

Output:

10,20,30,40,50
Copy the code

The filter () method

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

<script>
function isPositive(value) {  
    return value > 0;  
}
function func(a) {  
    var filtered = [112.52.0, -1.944] 
    .filter(isPositive);  
    document.write(filtered);  
}
func();  
</script>
Copy the code

Output:

112,52,944
Copy the code

Findindex () method

This method returns the index of the first element in the given array that satisfies the provided test functionality. 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 the code

Output:

2
Copy the code

The 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 the elements of the array, as well as the first element to print that meets the criteria.

<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 the code

Output:

30
Copy the code

The fill () method

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

<script>
// JavaScript code for fill() function  
function func(a) {
    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 the code

Output:

1.87.87.58
Copy the code

ForEach () method

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

<script>  function func(a) {      
// Original array      
const items = [1.29.47];      
const copy = [];      
items.forEach(function(item){          
copy.push(item*item);      });      
document.write(copy);  }  
func();  
<script>
Copy the code

Output:

1.841.2209
Copy the code

Sort () method

This method is used to sort an array. Arrays can be of any type, such as strings, numbers, characters, etc.

<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 the code

Output:

10,25,50,88
Copy the code

Concat () method

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

<script>
// JavaScript code for concat() function  
function func(a) {  
    var num1 = [11.12.13],  
        num2 = [14.15.16],  
        num3 = [17.18.19];
    document.write(num1.concat(num2, num3));  
}  
func();  
</script>
Copy the code

Output:

11.12.13.14.15.15.16.17.18.19
Copy the code

Include () method

This method is used to know if a particular element exists in the array, so it returns true or false, that is, true if the element exists and false otherwise.

<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 the code

Output:

true
Copy the code

The reverse () method

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

<script>  function func(a) {      
//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 the code

Output:

Raw array:34,234,567,4New reverse array:4,567,234,34
Copy the code