JS Array

Download (.odt) Download (Markdown)

Get items of array

[hook]

Return an item by index (starting from 0):

Return index of an item (first occurrence) using indexOf(item) method (returns -1 if item is not found):

Return the length of array using the length property (counts from 1):

Check if an item is in the array

[hook]

Using includes(item) method:

Using includes(item) method with two arguments (second is starting index):

Modify an item

[hook]

Modify an item on a specific index (indexes start from 0):

Add items to the start or end of array

[hook]

Add items to the start of array using unshift(item1, item2) method (can have limitless number of parameters):

Add items to the end of array using push(item1, item2) method (can have limitless number of parameters):

Remove a single item from the start or end of array

[hook]

Remove first item of array using shift() method (also returns the removed item):

Remove last item of array using pop() method (also returns the removed item):

Add or remove multiple items anywhere inside array

[hook]

Remove items using splice(startingIndex, numberOfItemsToRemove) method (creates and returns a new array with removed items):

Remove and add items at the same time using splice(startingIndex, numberOfItemsToRemove. itemToAdd1, itemToAdd2) method (there can be limitless number of itemToAdd parameters, creates and returns a new array with removed items):

(if you just want to add items without removing any, provide 0 to the second, numberOfItemsToRemove parameter)

Add items of array to another array

[hook]

Spread operator:

Iterate through the array

[hook]

Iterate through the array using for loop:

Using forEach(function(item){}) method:

Warning!

If you use return inside the callback function provided as a paramater to forEach then return only quits the callback function.

E.g.

The containsHelloWorld(array) function will always return false because if it finds the string "Hello World" it returns with true value from the callback function then it returns with false value from the containsHelloWorld(array) function (which is probably not what you want if you write this piece of code).

In this case it's better to use a for loop:

This code will always return the correct value because when it finds the string "Hello World" it returns with true value from the whole containsHelloWorld(array) function so it doesn't reach the second return.

Create a new array with modified version of items from original array using map(function(item){}) method:

Check a statement against all items of array using every(function(item){}) method (returns true if all items match the statement, otherwise it returns false):

Ordering items using sort() method (modifies original array)

[hook]

Put string items into alphabetical order - case-sensitive:

Put string items into reverse alphabetical order - case-sensitive:

Put string items into alphabetical order - case-insensitive:

Put string items into reverse alphabetical order - case-insensitive:

Put number items into ascending order:

Put number items into descending order:

Multidimensional array

[hook]

Return and modify items of embedded arrays:

Use array methods:

Iteration: