DEV Community

Daryl Carr
Daryl Carr

Posted on

A quick set of questions beginners can ask to know which javascript array method to use!

This is a quick one

As a beginner, one of the first tasks you must learn is to ask "what am I trying to achieve"? I found these simple questions helped me choose the correct array method for the job.

The Questions

  • Do I want only some of these items? Array.filter

  • Do I want to convert each item into something else? Array.map

  • Do I want to run some code for every item in the array? Array.forEach

  • Do I want to know if at least one item in an array matches some criteria? Array.some

  • Do I want to know if every item in an array matches some criteria Array.every

  • Do I want to get the first item from an array which matches some criteria? Array.find

  • Do I want to know if an item is in an array? Array.includes

  • Do I want to know the index of an item in an array? Array.indexOf

  • Do I want to sort the array items in a particular order? Array.sort

  • Do I want to reverse the order of the items in the array? Array.reverse

  • Do I want only some of the array and I know the indexes? Array.slice

  • Do I want the first or last "n" items in array? Array.slice

  • Do I want to join the items into a string? Array.join

  • Do I want to add 1+ items to the start of an array? Array.unshift

  • Do I want to add 1+ items to the end of the array? Array.push

  • Do I want to remove 1 item from the start of an array? Array.shift

  • Do I want to remove 1 item from the end of the array? Array.pop

  • Do I want to join 2+ arrays together? Array.concat

Summary

There are some notes to the above;

  1. These are nowhere near the complete list of Array methods but are the ones I use most in my day-to-day work.

  2. These methods are powerful and can not only to answer these simple questions but can do a lot more as well! But beware, some methods can also have unintended side effects.

  3. I've not included Array.reduce here but it's the one I use if I want to combine some of the above questions. It's also useful if I want more control over the output.

  4. Don't worry if you don't remember even half of these. I always have to re-visit documentation to remember how to use them!

  5. These methods are like a set of tools in a toolbox. Sometimes a range of tools can complete a task but the more tools you have, the better chance you can use the best one. Also, don't forget that you can combine tools (array methods) to achieve your goal. Composition is powerful!

These questions can help let you develop a sense of what methods are useful in which situations. Most the time it's a combination of the above methods enable the user to achieve their goal. For example, using filter and map to remove and transform one set of items into another. Combining these methods reads nicely and it's easier to understand when revisiting the code in the future.

So that's it. A real quick post! Hopefully you found this useful :)

Which array methods do you use most? Are there any missing from the above that you can't live without?

Top comments (0)