DEV Community

Bhupesh Chandra Joshi
Bhupesh Chandra Joshi

Posted on

Array methods : Every Developer must Know

There are plethora of array methods ,this article covers the most important methods. We will categories array methods into two parts.

1- Methods which modify the orignal array
2- List of methods those leave array unchanged.

Underscore.js provides top 100 functions that supports your workplace and contains the methods which we are nominated as iterator methods, and these work on objects and arrays.

Note: Everything is object in javascript.
1-each() a.k.a forEach()
2-map()
3-reduce()
4-include()
5-filter

Stack based methods in JavaScript:

1-push() 2-pop() 3- shift() 4- unshift()

Iterator methods loop over the element of an array. Our days and nights are excellent example of loop, loop means over the time we repeat the element. and our day and night is 24 hrs loop.

Iterator methods work over a function ,as we know our days work over sun and night works over moon.

1-each() a.k.a forEach()

1- It requires a callback (a.k.a arrow function) with three elements

2- We sub-divide the callback function and mention the accepted parameters of a callback function.

1- element
2- index
3- array

Our first value to callback function is mandatory and the array element ,you must pass, if you want the total of the price of books, what you do,let's look up the code. copy the code on "google console"

Before the forEach function ,our array state :

price=[1000,2000,3000,4000];

After the forEach, our array state will not modify:
the array will be price=[1000,2000,3000,4000];

MAP()

1-You will Leverage the map function for your maximum advantage,
map returns a new array without modifying the orignal array,it is invoked on.

2- New array will be the same length as your orignal array.

3- map() is similar to forEach ,and it accepts the same number of parameters and first element on callback function is Obligatory and you must pass the first element of an array.

today ,we have list of fruits and the demand of city increases the twice, so we will order the 2*price with help of map.

Before map function,our array state.

price=[1000,2000,3000,4000];

After executing the map function ,the result will become:We will use the double logic in the map.
price=[2000,4000,6000,8000];

If you will engaging your map function with empty callback ,so it will return the empty new array.

Map function , provides you a way to modify the array and we can double the array and the orignal array will remain intect.

Before the map ,the values of orignal array.

filter()

1- It will captivate the attention of javaScript interpreter and accepts a callback and after passing the test ,returns the new array without modifying the orignal one.

2- filter creates a shallow copy of the array part which passes the condition

Array.prototype.reduce()

We pass a callback function on reduce and we pass a initialValue which is intially the value of accumulator and summarise the array value into one value.

Top comments (0)