Arrays

Learn how to achieve immutability principles with objects.

We'll cover the following

Arrays are a bit trickier because they have multiple methods for adding and removing values. In general, you have to remember which methods create a new copy of the array and which change the original one.

Methods

The following table outlines the basic array methods:

Safe methods Mutating methods
concat() push()
slice() splice()
map() pop()
reduce() shift()
reduceRight() unshift()
filter() fill()
reverse()
sort()
copyWithin()

The basic array operations in most reducers are appending, deleting, and modifying an array.

To keep to the immutability principles, we can achieve these using the following methods:

The following code shows how to append to an array:

Get hands-on with 1400+ tech skills courses.