JavaScript Array Iteration Methods
Last Updated :
21 Jul, 2023
JavaScript Array iteration methods perform some operation on each element of an array. Array iteration means accessing each element of an array.
There are some examples of Array iteration methods are given below:
The array.forEach() method 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 an 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: The item index is the index of the current element which was being processed by the function.
- array: The array on which the .forEach() method was called.
Example: This example uses the forEach() method on an array for iterating every element of an array and printing every element of an array in a new line.
JavaScript
let emptytxt = "";
let Arr = [23, 212, 9, 628, 22314];
function itrtFunction(value, index, array) {
console.log(value);
}
Arr.forEach(itrtFunction);
The array.some() method checks 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 method accepts three parameters as mentioned above and described below:
- value: This is the current value being processed by the function.
- index: The item index is the index of the current element which was being processed by the function.
- array: The array on which the .some() method is called.
Example: This example checks all value of the array, if some value is greater than 50 then it returns true and if all elements of an array are less than 18 then it returns false.
JavaScript
let Arr1 = [41, 2, 54, 29, 49];
let someOver50 = Arr1.some(myFunction1);
console.log("Are some values over 50: "
+ someOver50);
function myFunction1(value, index, array) {
return value > 50;
}
let Arr2 = [41, 2, 14, 29, 49];
let allLessThan18 = Arr2.some(myFunction2);
console.log("Are all values less than 18: "
+ allLessThan18);
function myFunction2(value, index, array) {
return value < 18;
}
OutputAre some values over 50: true
Are all values less than 18: true
The array.map() method creates an array by calling a specific function on each item in the parent array and it does not change the value or element of the array.
Syntax:
array.map(function(value, index, array){
}[ThisArgument]);
Parameters: This method accepts three parameters as mentioned above and described below:
- value: This is the current value being processed by the function.
- index: The item index is the index of the current element which was being processed by the function.
- array: The array on which the .map() function was called.
Example: This example performs a sum operation on every element of the array and displays output. It does not change the values of the original array.
JavaScript
let numArray = [1, 2, 3, 4];
let numArray2 = numArray.map(multiplyFunction);
console.log(numArray2);
function multiplyFunction(value, index, array) {
return value + 100;
}
Output[ 101, 102, 103, 104 ]
Similar Articles
All these are the array iterator functions.
Similar Reads
ArrayList iterator() method in Java with Examples The iterator() method of ArrayList class in Java Collection Framework is used to get an iterator over the elements in this list in proper sequence. The returned iterator is fail-fast. Syntax: Iterator iterator() Parameter: This method do not accept any parameter. Return Value: This method returns an
2 min read
String Arrays in Java A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created.When we create an array of type String in Java, it is called a String Array
5 min read
ArrayList toArray() method in Java with Examples The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.Declaring toArray() methodpublic Object[] toArray() or public <T> T[] toArray(T[] a)Parameters: This method either accepts no parameters or it takes an array T[] as a para
3 min read
One Dimensional Array in Java An array is a type of data structure that can store a collection of elements. These elements are stored in contiguous memory locations and provide efficient access to each element based on the index of each array element.In this article, we will learn about a one-dimensional array in Java.What is an
7 min read
How to Return an Array in Java? An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key. The elements of the array are stored in a way that the address of any of the elements can be calculated using the location of
5 min read
CopyOnWriteArrayList set() method in Java with Examples The set(E e) method in the class CopyOnWriteArrayList class replaces the element at the specified index with the element provided as a parameter to the method. The method returns the element that has been replaced by the new element.Syntax: public E set(int index, E element) Parameters: The method t
2 min read
Reverse an Array in Java Reversing an Array is a common task in every programming language. In Java, there are multiple ways to reverse an array. We can reverse it manually or by using built-in Java methods. In this article, we will discuss different methods to reverse an array with examples.Let us first see the most common
4 min read
CopyOnWriteArrayList toArray() method in Java The toArray() method of CopyOnWriteArrayList is used to return an array containing all the elements in CopyOnWriteArrayList in the correct order. Syntax: public Object[] toArray() or public <T> T[] toArray(T[] a) Parameters: This method either accepts no parameters or it takes an array T[] a a
2 min read
How to Initialize an Array in Java? An array in Java is a linear data structure that is used to store multiple values of the same data type. In an array, each element has a unique index value, which makes it easy to access individual elements. We first need to declare the size of an array because the size of the array is fixed in Java
5 min read
Java Collection toArray() Method The toArray() method of Java Collection returns an array containing elements that are inside the calling collection. This article will discuss the toArray() method, its syntaxes, how it works, and some code examples. Syntax of toArray() MethodObject[] toArray();Return Type: The return type of the ab
3 min read