DEV Community

Cover image for Five powerful JavaScript array methods.
Taslan Graham
Taslan Graham

Posted on • Edited on • Originally published at taslangraham.com

Five powerful JavaScript array methods.

Full article can be found here.

Arrays are very common data structures in programming and is supported by many modern programming languages. In general, an array is collection of items stored at contiguous memory locations. Is most programming languages, these items are the same type. However, In JavaScript, elements stored within an array does not have to be of the same type.

JavaScript provides a vast number of methods that can be performed on arrays. In this article I'll discuss five methods that can improve the way you use arrays in JavaScript.

Creating an array in JavaScript

JavaScript allows you to create an array in two different ways.

Method 1

let arr = new Array();

Method 2

let arr = []

Both methods does the same thing, however, for execution speed, simplicity and readability, use the second method(the array literal method).

Array Methods

Now let's look at five powerful JavaScript array methods. Using these methods will definitely improve your codes readability and speed.

we'll use the following array in our examples:

This is an array that holds an object at each index,

filter() method

The filter() method creates a new array containing all the elements that passes the test implemented by the callback function provided in the filter() method. Let's take a look.

Inside the callback function, the ticketPrice of each array element in checked to see if it is less than 100. If the callback function returns true then this element is added to the new array.

map() method

With the map() method a function is called once for each element in the array. The results from the function called is used to create a new array.

The map() method can be useful when you want to create a new array using values from an existing array or you want to extract specific values from within an array.

Let's say we want to get the name of every movie. We'd do the following.

Inside the callback function which is executed for each element in the movies array, we return the name of the movie found at each element. This is then stored inside the movieNames array.

find() method

The find() method returns the value of the first element in an array that passes a test (provided as a function[callback]).

What if we wanted the information for the movie Queen & Slim? We'd do the following.

The find() method will return the value of the first array element that the callback function returns a true value for. Once a true value is returned, it does not check the remaining elements within the array.

forEach() method

forEach() method works similarly to a for-loop and can be used instead of a for-loop. A function is called once for each element of the array. You can then perform any kind of operation on the elements of the given array. The forEach() method is different from the previous methods in that it does not create a new array.

Let's loop through the movies array and print the name of each movie.

Using the forEach() method makes it much easier and cleaner to loop through an array.

reduce() method

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value which is stored in an accumulator.

What if we wanted to calculate the total of all movie ticket price (ticketPrice)? You could loop through the array using a for-loop or a forEach() and sum the ticketPrice of all element in the array. However, you could do this in a much easier way using the reduce() method.

This is how it is done.

The reducer function's returned value is assigned to the accumulator, whose value is remembered across each iteration throughout the array, and ultimately becomes the final, single resulting value.

The 0 is the initial value given to the accumulator.

There you have it, five methods that will without a doubt improve the way you use arrays in JavaScript. Using the filter(), map(), find(), forEach() and reduce() method on your arrays will allow you to do more with less code and great efficiency.

Thanks for reading! Until next time, Think, Learn, Create, Repeat!

View original post

Sources

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

https://www.w3schools.com/jsref/jsref_obj_array.asp

https://www.geeksforgeeks.org/javascript-array-map-method/

https://www.geeksforgeeks.org/javascript-tutorial/#array

Buy Me A Coffee

Top comments (12)

Collapse
 
itsjzt profile image
Saurabh Sharma
Array(10).fill(null).map(_, i => i)
Enter fullscreen mode Exit fullscreen mode

This creates an array of the given range 10 in this case. I use this all the time

Collapse
 
1987cr profile image
Carlos Riera

Shorter alternative :)

[...Array(10).keys()]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
itsjzt profile image
Saurabh Sharma

Oh nice,

Collapse
 
taslangraham profile image
Taslan Graham

Wow!
I've never used this before. Will give it a try soon.

Collapse
 
octocodeio profile image
Wojciech Dasiukiewicz

For me, one of the top useful functions from Array object is Array.from(). You can transform for example NodeList to regular Array of Objects

Collapse
 
jamesthomson profile image
James Thomson

You can transform for example NodeList to regular Array of Objects

You can also do this using the ES6 spread operator, e.g. const arr = [...document.querySelectorAll('div')];

Collapse
 
_merksam profile image
Evgeniy Viniychuk

I guess we also shouldn't forget about some and every methods, they give you a great possibility to check complex conditions and are useful sometimes.

Collapse
 
mugas profile image
Ricardo Moreira

Good post Taslan

Collapse
 
taslangraham profile image
Taslan Graham

Glad that you liked it, Ricardo

Collapse
 
naveenkumar337 profile image
naveenkumar337

This is great article, to explain mostly used Array methods

Collapse
 
taslangraham profile image
Taslan Graham

Glad that you found it useful!

Collapse
 
anselmegildas profile image
AnselmeGildas

Salut avec quoi (quelle application) vous réalisez les créas pour présenter les bouts de code.