loading...

My Top 3 JavaScript Array Methods

georgehanson profile image George Hanson Originally published at georgehanson.co.uk ・2 min read

JavaScript is a super versatile language. I love it and it really has come a long way over recent years. With JavaScript, we can now build powerful single page applications and when building these applications you will at some point or another use arrays.

I love arrays, here are my top three JavaScript methods that I use on a regular basis.

1. Array Filter

This is actually a method that was added back in ES5, but it is still a method that I use on a regular basis. Array filter allows us to basically do what it says on the tin. Filter an array to a subset based on rules that we provide.

To do this, you simply call the filter method on the array and pass it a callback. For example in this array, I am filtering the results to only return objects where the age is less than 18.

const people = [
  { name: 'Jon', age: 23 },
  { name: 'Daenerys', age: 23 },
  { name: 'Arya', age: 17 }
]

const children = people.filter(person => person.age < 18)
// Returns [ { name: 'Arya', age: 17} ]

2. Array Find

This is something that was added to the ES6 specification. It is similar to the array filter method, but it instead returns us the first result from the filtered array.

For this method again we pass it a callback, containing the rules for our filter. It will then filter the results and return us the first item in the subset. In this case, I want to find the first adult in the array of people.

const people = [
  { name: 'Jon', age: 23 },
  { name: 'Daenerys', age: 23 },
  { name: 'Arya', age: 17 }
]

const adult = people.find(person => person.age >= 18)
// Returns { name: 'Jon', age: 23 }

You'll notice that this time it returns the object, as opposed to an array containing the object.

3. Array ForEach

The forEach method is one that was introduced back in ES5 and therefore has ample browser support. However, I do still see code where people are not using this and are instead using a for loop.

The forEach method does exactly what you think, it loops over each item in the array for you to run a callback on. This is similar to the map method, but there is one key difference. When you use array map, it returns a new array based on the callback you have passed it. Whereas with the forEach method it returns nothing.

Both the map and forEach methods have their use cases. But if I am not intending on manipulating the array I will generally reach for the forEach method.

In the example below I am simply calling the forEach method on the array and passing the object to a custom function.

const people = [
  { name: 'Jon', age: 23 },
  { name: 'Daenerys', age: 23 },
  { name: 'Arya', age: 17 }
]

people.forEach(person => sendNotification(person))

Conclusion

There you go, there are my three favourite array methods. Don't get me wrong there are plenty of other array methods that are really useful and I implore you to look them up. But these are three of my favourites and the three I most commonly use.

Do you have any different array methods that you frequently use? Be sure to let me know.

Posted on by:

georgehanson profile

George Hanson

@georgehanson

I'm a full stack web developer based in the United Kingdom. I'm usually writing a lot of PHP or JavaScript 🛠️

Discussion

markdown guide
 

Mine would probably be map(), sort() and filter(), with an honourable mention for reduce().

Because I use React a lot, I often have to loop through a list of sub components to render. For that, map() is indispensable in transforming an array from a list of Javascript objects into React components. It's also common to have to sort and filter them.

On occasion I also need to produce some form of aggregate from array data, and the combination of map() and reduce() can be very powerful for that.

 

+1 for map. It's a really great and clean method to transform array data structures. Use it all the time, no matter if it's a React component or a vanilla javascript.

 

The forEach has come up a lot. I'll throw a reminder in here of its performance hit (minor) and more-so, something people might not know is that the forEach is not truly Iterable... hence, continue and break statements don't work. Knowing that tidbit can determine when and when not to use the forEach.

 

The reason why people would use a for loop over a foreach loop is for speed. For loops perform much faster than a foreach

 

Unless you are looping over massive arrays, it doesn't really matter in my opinion. I'd much value readability over a couple of milliseconds. If it proved to be a performance issue in the grand scheme of things it could always be replaced with a for loop.

 

Agreed, I'll almost always opt for forEach. Though for allows you to break out early, which is nice when needed.

Actually this is also possible with forEach, since a function is called for each element. just do a return

EDIT: This was a misunderstanding of the desired behavior. Using return will just skip the current element and continue with the rest of the array, see my comment below

You sure about that? AFAIK forEach will continue to run until the end of the array.

jsfiddle.net/jamesbrndwgn/mz0ynkvb/

Unless there's some trick I'm not aware of?

Ah I'm sorry I understood you wrong. I thought you meant skipping an element like continue. You meant completely stop after a certain element. This is currently not possible with forEach, but you could use for (obj of array) or some instead to achieve that behavior
Sorry again for the misunderstanding