JavaScript Tutorial

Javascript Array Iteration

Array iteration is a process that allows you to perform operations on each item within an array. It enables you to apply a specific action or execute a function for every element in the array. In JavaScript, there are several methods available for array iteration.

Let's explore a few commonly used methods:

Array.forEach()

The forEach method executes a provided function once for each element in the array. It is represented by the following syntax:-

array.forEach(callback(currentvalue,index,arr),thisArg)

It can have the following parameter:-

  • callback – The callback parameter represents the function that tests the condition.
  • currentvalue – It represents the current element of the array.
  • index - It is optional. Index defines the index of the current element.
  • arr - It is optional. Arr defines the array on which forEach() operated.
  • thisArg - It is optional. The value to use is this while executing callback.

Input:-

Output:-

 

Array.map()

This method is used to transform elements in an array. It creates a new array by performing a function on each array element.

It does not execute the function for array elements without values.

Points to remember:-

  • A map object cannot contain duplicate keys.
  • A map object can contain duplicate values.
  • The key and value can be of any type (allows both object and primitive values).
  • A map object iterates its elements in insertion order.

It is represented by the following syntax:-

new Map([iterable])   

It can have the following parameter:-

  • iterable – this parameter represents an array and another iterable object whose elements are in the form of key-value pair.

Input:-

Output:-

Array.filter()

This method creates a new array with array elements that pass a test. It doesn't change the original array.

One of the most common tasks when working with an array is to create a new array that contains a subset of elements of the original array.

It is represented by the following syntax:-

array.filter(callback(currentvalue,index,arr),thisArg)     

It can have the following parameter:-

  • callback – The callback parameter represents the function that tests the condition.
  • currentvalue – Currentvalue represents the current element of the array.
  • index - It is optional. The index represents the index of the current element.
  • arr - It is optional. Arr represents the array on which filter() operated.
  • thisArg - It is optional. ThisArg is the value to use as this while executing callback.

Input:-

Output:-

 

Array.reduce()       

The reduce method applies a function to reduce the array to a single value by iteratively combining its elements.

It is represented by following syntax:-

arr.reduce(callback(accumulator, currentValue, currentIndex, array), initialValue)       

It can have the following parameter:-

  • callback: The callback function is executed for each element, excluding the first, if no initialValue is specified.
  • initialValue: InitialValue is the first argument value used in the first invocation of the callback function.

It returns a single value as an output.

Input:-

Output:-

 

Array.indexOf()

The Array.indexOf() method is used to search for a specific element value within an array and returns its position.

The index positions in an array start at zero, meaning the first element has an index of 0. If the element is not found in the array, the method returns -1.

It is represented by the following syntax:-

array.indexOf(element,index)         

It can have the following parameter:-

  • element:  It represents the element to be searched.
  • index:  It represents the index position from where the search starts. It is optional.

Input:-

Output:-

 

Array.lastIndexOf()

The Array.lastIndexOf() method is used to search for the position of a specific element within an array. It functions similarly to the indexOf() method but starts searching from the last position of the array.

The lastIndexOf() method is case-sensitive, meaning it considers the exact casing of the element. The index position of the first character in a string is always 0. If the element is not found in the array, the method returns -1.

Here is the syntax for Array.lastIndexOf():

array.lastIndexOf(element,index)           

It can have the following parameter:-

  • element:  It represents the element to be searched.
  • index:  It represents the index position from where the search starts. It is optional.

Input:-

Output:-

 

Go back to Previous Course