Arrays are admirable and a very accurate type in JavaScript. There are many useful congenital backdrop and methods that will help you boldness any task which involves this kind of type. Today, we’re going to altercate 15 array methods every developer should know. So, let’s get started:

  • some()
  • every()
  • reduce()
  • map()
  • flat()
  • filter()
  • forEach()
  • findIndex()
  • find()
  • sort()
  • concat()
  • fill()
  • includes()
  • reverse()
  • flatMap()

This list is not abundant as I don’t accept one method is more important than the other. Each method will boldness a altered problem, so it’s important we’re accustomed with all of them.

some()

The some() tests whether at least one aspect in the array passes the test implemented by the callback function. The callback action will accept three arguments: the item, the index, and the full array. Additionally, it’s accessible to assign a value for this when active the callback by using the altercation thisArg.

Definition:

webrok

Examples:

webrok

every()

The every() method is, in a way, agnate to the some() method, but it tests whether all the elements in the array pass the test implemented by the callback function.

Definition:

webrok

Examples:

webrok

reduce()

The reduce() method executes a callback action once for each assigned value present in the array, taking four arguments:

  1. accumulator
  2. currentValue
  3. currentIndex
  4. array

The first time the callback is called, accumulator and currentValue can be either the initialValue if provided, and the first value in the array if not.

Definition:

webrok

How does reduce() work?

Let’s see with an archetype of how reduce() works:

webrok

If we go step by step and put in a table all the ambit plus the consistent value of the callback, we would get the following:

webrok

const initialValue = 10And the final result would be 10. In our accurate case, I did not accommodate an antecedent value. Let’s try that next:

webrok

With this new scenario, our table would like this:

webrok

The reduce() action is great and it has several uses like accretion all the values of an array or in an object array, counting for accurate items in the array, alignment objects, amalgamation arrays independent in an array of objects, removing duplicates, etc. And the final consistent value is 20.

map()

The map() method creates a new array busy with the after-effects of the callback action for each aspect in the array. Agnate to the other methods, the callback action will accept three arguments: currentValue, index, and array. As is with the case of reduce(), the callback is only invoked for indexes of the array which have assigned values (including undefined).

Definition:

webrok

Always be accurate when using map().Remember that on each call will create a new array. If you don’t absolutely need the array and you are simply trying to iterate, use forEach() or for-of instead.

Examples:

webrok

As we mentioned map() will create a new array, so the afterward is a aftereffect of that:

webrok

Even though each array contains the exact same elements, they are not the same advertence and thus the numbers === numbers2 resolves to false.

flat()

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the defined depth. By default, it will abrade one level.

Definition:

webrok

Examples:

webrok

Note that if we want to abrade all levels recursively, we can pass Infinity as the altercation of the function.

filter()

Together with map() I think this is one of my favorites. The filter() method creates a new array with all the elements that pass the test implemented by the callback function.

Definition:

webrok

Examples:

webrok

forEach()

The forEach() method executes a provided action once for each array element.

Definition:

webrok

Example:

webrok

There are two important considerations when using forEach()

  • There is no way to stop or break a forEach() loop other than throwing an exception.
  • forEach() expects a ancillary callback, and it won’t wait for promises to be resolved.

Let’s see an archetype of the latter:

webrok

Even though we would have accepted the capricious sum to have accumulated all the values in the array and have a value of 14, the output was 0 as the forEach() account ended after apprehension for the promises, and thus the console.log account was accomplished before the capricious sum was updated. So be very aware of this bearings as it can lead to your code, bearing abrupt results.

findIndex()

The findIndex() method allotment the index of the first aspect in the array that satisfies the provided callback function. Otherwise, it allotment -1, advertence that no aspect passed the test. Unlike other methods, findIndex() will assassinate the callback action even for indexes with unassigned values.

Definition:

webrok

Example:

webrok

find()

The find() method is agnate to the findIndex() method. However, it allotment the value of the first aspect which satisfies the provided callback action as declared to its index. If no aspect satisfies the callback, then undefined is returned.

Definition:

webrok

Example:

webrok

sort()

The sort() action is very common and simply allows us to sort the elements of an array in place and abiding the allocation array. The absence sort order is ascending. The complication and achievement of this method cannot be guaranteed, as it depends on the implementation.

Definition:

webrok

Examples:

webrok

Always bethink that the allocation happens in place, so:

webrok

The sort action will modify the absolute array, and return a advertence to the same array, and thus the aboriginal array and the alternate array will be the same.

concat()

The concat() method is used to merge two or more arrays into a new array.

Definition:

webrok

Examples:

webrok

fill()

The fill() method changes all the elements in an array to a static value, from a start index (default 0) to an end index (default array.length). The updates will happen in place and will return a advertence to the same array.

Definition:

webrok

Examples:

webrok

includes()

The includes() method determines whether an array includes a assertive value among its entries, abiding true or false. Note that the method includes() is case-sensitive when comparing strings and characters.

Definition:

webrok

Examples:

webrok

reverse()

The reverse() method reverses an array in place. By abandoning we mean that the action will alter the elements of the array, the first aspect will become the last, and the last the first element. This operation will mutate the aboriginal array and return a advertence to the same.

Definition:

webrok

Examples:

webrok

flatMap()

The flatMap() method applies a action to each aspect of the array and then abrade the result into an array. It combines flat() and map() in one function.

Definition:

webrok

Example:

webrok

Summary

JavaScript arrays come with some great methods that can abridge our development efforts. Knowing them can save us time and, in some cases, even boost the achievement of our code. I hope that today you abstruse some new array methods, or active old concepts that you can use for your next project.

Read next: To Gmail, Black Lives Matter emails are 'promotions'

Celebrate Pride 2020 with us this month!