Hey there. Hope you are doing well😊
In the last post we have studied about the searching and sorting methods used in array. In this post we are going to discuss about Iteration Methods used in array.
So let's get started🔥
Iteration Methods
Array iteration methods are functions or techniques used to traverse through each element of an array or collection of elements systematically. These methods are commonly used to perform operations on each element of an array without needing to manually write loops.
Array.forEach()
Method
The forEach()
method calls a function (a callback function) once for each array element.
So here you can see that the print
function is called for every element present in array.
This callback function used here uses three arguments -:
value
index
array
But these are optional we can provide arguments according to our need.
Array.map()
Method
Creates a new array populated with the results of calling a provided function on every element in the calling array.
The callback function defined here can accept three arguments-: value, index, array.
Array.filter()
Method
Creates a new array with all elements that pass the test implemented by the provided function.
The callback function defined here can accept three arguments-: value, index, array.
Array.reduce()
Method
The reduce()
method runs a function on each array element to produce (reduce it to) a single value.
The reduce()
method works from left-to-right in the array.
Here the accumulator and currentValue are initialized to 0, accumulator stores the total sum and currentValue is initial value of accumulator.
We have reduceRight()
which works similar to reduce()
but works from right to left.
Array.every()
Method
Tests whether all elements in the array pass the test implemented by the provided function.
Array.some()
Method
Tests whether at least one element in the array passes the test implemented by the provided function.
Array Spread (...)
The...
operator expands an iterable (like an array) into more elements.
The spread operator provides a concise and flexible way to work with iterables, making code more readable and allowing for cleaner syntax in various situations.
These methods explained above are very important methods. They are most commonly used methods.
I hope you have understood this blog. In the upcoming blogs we are going to read about OOPs. Till then stay connected and don't forget to follow me.🤍
Top comments (0)