Arrays

Array

It is just an object that offers operations to access and manipulate its properties.

arr.length: Shows the number of elements inside the Array, but it also considers empty positions.

  • Then, it would show a wrong number of used positions.

  • Thus, avoid using delete to remove elemnts from the Array.

Mutator methods

They modify the Array.

arr.push(el): Add element to the end.

arr.pop(): Remove element from the end.

  • [Also returns the removed element]

arr.unshift(el): Add element at the beginning.

arr.shift(): Remove element at the beginning.

  • [Also returns the removed element]

arr.splice(): Removes, substitutes ou add, one or more elements in a specifig position.

  • [Also return the removed elements]

arr.sort(fn): Sorts the elements according to a function.

  • [-1 and 0 the element stays where it is; 1 it inverts with the other one]

  • [It changes the Array in place, does not return a new one]

arr.reverse(): Reverse the elements order.

arr.fill(): Fills (all or specific interval) of the values with the specified value.

Array iteration

They iterate over Array values.

arr.forEach(fn): Execute the given function for each element.

arr.filter(fn): Returns a new array with only the filtered elements.

arr.find(fn): Returns the first element that returned true in the given function.

arr.some(fn): Returns true if one or more elements returned true in the given function.

arr.every(fn): Returns true if all the elements returned true in the given function.

arr.map(fn): Returns a new array based on the return of the given function.

arr.reduce(fn(result, current) => {}): Return a value based on the return of the given function.

Accessor methods

They return specific information about the Array.

arr.indexOf(): Returns the position of the first found element.

arr.lastIndexOf(): Returns the position of the last found element.

arr.includes(): Return true if the given element exists.

arr.concat(): Returns a new array result of the concatenation.

arr.slice(): Returns part of an array, given a [start, end) position.

arr.join(): Converts the array to a string, joining the elements with a given separator.

Last updated