JavaScript | Array Iteration Methods

Array iteration methods perform some operation on each element of array. There are some different examples of Array iteration methods are given below.

Array.forEach() function: The array.forEach() function calls the provided function(a callback function) once for each element of the array. The provided function is user defined, it can perform any kind of operation on array.

Syntax:



array.forEach(function callback(value, index, array){
}[ThisArgument]);

Parameters: This function accepts three parameters as mentioned above and described below:

  • value: This is the current value being processed by the function.
  • index: Item index is the index of the current element which was being processed by the function.
  • array: The array on which the .forEach() function was called.

Example: This example uses forEach method on array for iterating every element of array and printing every element of array in new line.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        JavaScript Array.forEach() function
    </title>
</head>
  
<body style = "text-align:center;"
  
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1>
  
    <h2>Array.forEach() method</h2>
      
    <p>
        array.forEach() method Calls a function
        once for every array element.
    </p>
      
    <p id="GFG"></p>
      
    <script>
        var emptytxt = "";
        var No = [23, 212, 9, 628, 22314];
        No.forEach(itrtFunction);
          
        document.getElementById("GFG").innerHTML = emptytxt;
          
        function itrtFunction(value, index, array) {
            emptytxt = emptytxt + value + "<br>"; 
        }
    </script>
</body>
  
</html>                    

chevron_right


Output::

Array.some() function: The array.some() function check whether at least one of the elements of the array satisfies the condition checked by the argument function.

Syntax:

array.some(arg_function(value, index, array), thisArg);

Parameters: This function accepts three parameters as mentioned above and described below:

  • value: This is the current value being processed by the function.
  • index: Item index is the index of the current element which was being processed by the function.
  • array: The array on which the .some() function was called.

Example: This example checks all value of array, if some value is greater than 50 then it return true and if all element of array is less than 18 then it return false.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
      
<head>
    <title>
        JavaScript Array.some() function
    </title>
</head>
  
<body style = "text-align:center;"
  
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1>
  
    <h2>Array.some() method</h2>
      
    <p>
        some() method checks some array elements 
        validity according to the test condition.
    </p>
      
    <p id="GFG"></p>
      
    <script>
        var numArray = [41, 2, 14, 29, 49];
        var someOver50 = numArray.some(myFunction);
          
        document.getElementById("GFG").innerHTML
            = "Some values over 50 is " + someOver50;
          
        function myFunction(value, index, array) {
            return value > 50;
        }
    </script>
</body>
  
</html>                    

chevron_right


Output:

  • If any one value of array is greater than 50:
  • If all value of array is less than 50:

Array.map() function: The array.map() function creates an array from calling a specific function on each item in the parent array and it does not change the value or element of array.

Syntax:

array.map(function(value, index, array){
}[ThisArgument]);

Parameters: This function accepts three parameters as mentioned above and described below:

  • value: This is the current value being processed by the function.
  • index: Item index is the index of the current element which was being processed by the function.
  • array: The array on which the .mao() function was called.

Example: This example perform sum operation on every element of array and display output. It does not change the values of original array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Array.map() function
    </title>
</head>
  
<body style = "text-align:center;"
  
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1>
  
    <h2>Array.map() method</h2>
      
    <p>
        creates an array from calling a specific 
        function on each item in the parent array.
    </p>
      
    <p id="GFG"></p>
      
    <script>
        var numArray = [1, 2, 3, 4];
        var numArray2 = numArray.map(multiplyFunction);
          
        document.getElementById("GFG").innerHTML = numArray2;
          
        function multiplyFunction(value, index, array) {
            return value + 100;
        }
    </script>
</body>
  
</html>                    

chevron_right


Output:

Similarly, Array.findIndex() Method, Array.find() Method, Array lastIndexOf() function, Array indexOf() function, Array every() function, array.reduceRight() function, Array reduce() Method and Array filter() function are the array iterator functions.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.