5 JavaScript Array Methods to make your code lean and mean
Tags: javascript, arrays, efficiency, es6
Published: 2021-02-13 23:19:00

One of the most important aspects of programming in JavaScript is learning how to manipulate data within arrays.  The good ol "for loop" is a tried and true way to iterate through an array, but there are much easier, cleaner, and more efficient ways to handle the same data.  Still iterating through arrays only using the for loop?  Totally fine.  It is important to understand what's going on under the hood, but once you have a firm grasp on the foundational for loop, I strongly recommend moving onto these array methods. There are many different array methods, but I will be covering 5 different methods that you will likely see and use often.  Let's take a look. 
**Note**: *I will use both es6 and pre-es6 syntax, in order to show the differences and to offer a better understanding of the methods themselves.*

Sample dataset for demonstration purposes:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ti6vw7kxj4r9d6ousduo.png)



#1. map()
The map method is one of the most frequently used array methods out there.  In short, the map method returns a new array with whatever values specified by the developer. For example, if we wanted to only grab the names of the items from the items array, we can use the map method. Let's see how it works.

Without array methods like map, we would have to do something like this: ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hy9u0h7bkef1q2vopihi.png)

Does it work? Sure. But there are much cleaner ways to achieve the same result! In the long run, this saves time, memory, and enhances the readability of your code, which is beneficial for everyone involved.

####Pre-ES6 Syntax: ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/of8irarljudkxlc0kymt.png)

So what's going on here?  We are declaring a variable called "itemNames" and inside of it is where all of the magic happens.  All you have to do is take the name of the array, tack a .map() method onto it, and pass in a callback function as an argument.  The callback function also takes a parameter, and this can be named whatever you like. In this case, it made sense to use the word "item" for iterating through our items array.  Since we want the names of the items, we are simply returning item.name.  This will create a brand new array (which can now be accessed by using the "itemNames" variable) containing the names of the items.
####Output:
 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1svwirbnw0kummdmf4pz.png)

Let's break this down even more.
####ES6 syntax: ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pggwaql24atwuz4lb9v2.png)

Thanks to ES6, we can consolidate this into ONE single line of code and achieve the same result. Pretty cool.  Okay, so that's great, but what's going on here? Since we are using an ES6 arrow function and there is only one expression being executed in the callback function, we are able to eliminate the curly braces and even the return keyword(implicit return).

#2. forEach()
  forEach is a bit different in that it does not return an array like map, filter, etc.  So what does forEach do?  When an anonymous or callback function is passed to the forEach method, it will execute that function for every item in the array. You can also pass in the index of the array item, and even the entire array itself. For this use case, we want to iterate through the items array, and console.log the item name and which position it is in. We can achieve this with a forEach method. ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fp248u0kg09u88zh9smi.png)

####Output: ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oo0qqfmrzgrf09h372lk.png)

Let's clean this up a little bit with some ES6 syntax.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4y74gu18g6kfx05mp0l4.png)

Arrow functions are your friend!

#3. filter()
The filter method is one that you will grow to absolutely love.  Throughout your development career, there will be many use cases where you will want only certain items in an existing array, and filter is the way you can execute that. Just like map, the filter method returns an array. *However*, the new array is constructed by conditionals set by the developer.  Say we wanted to remove items from the array that are over $150: ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kzsw60u6hkd5ur9i7get.png)

####Output: ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h4zio1zbmliluuu48fgp.png)
So on every iteration, the filter method is looking at the price of each item, and if the conditional is met (if the price is less than or equal to 150), the item stays, if not, the item is tossed from the array.

Let's clean it up with ES6 syntax: ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/csv13warl57ttbbd27wl.png)

#4. reduce()
This can be a tricky method to learn at first, but once the concept solidifies, it is extremely useful.  Imagine we want to add up the prices of our items to get an idea of how much all of our assets are worth.  We can use the reduce() method to "reduce" the prices in the array to one single value.  Let's take a look: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bs63c9r0qgm5bqh2mvx4.png)

Now, the callback function in a reduce method normally takes two parameters.  One is the accumulator, and the other is the current value.  In this example, they are represented as "a" and "c".  So essentially, on every iteration, the current value is being added to the accumulator (accumulator is set to 0 by passing 0 as a second argument to the reduce method), and then ultimately returned as one single value.

####Output: ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l2uuzx16z1gb221xlcrx.png)

*It is important to note that if an initial value is not provided for the accumulator, it will default to the value of the first index of the array.*

Let's clean this up. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/odzx760kd5796s8x4j8c.png)
Less code, same results.

#5. find()
For the 5th and final method, let's talk about find().  Find is a super simple, yet useful method.  Essentially, it scans the array and returns the first object that meets certain criteria.  For example, if we wanted to isolate the object containing the "TV" item, we would write our code like this. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l5wsvka3unxkephah8si.png)

Output: ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1cqxkft9yb20i4esyn3u.png)

And of course, let's clean it up with ES6:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r62ben1j9u95t3ogd308.png)

####Conclusion
There are many different array methods out there to dry out your code and simplify your life as a developer, so take advantage of them! There's no better feeling than converting a clunky for loop into an efficient one-liner.  I hope this helped solidify some of the foundational concepts of array methods and ES6 syntax.  Stay well and keep coding!