Js Array Common Method 1
1. Join
The join()
method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
2. reverse
The reverse()
method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
Reverse is destrutive
3. sort
The sort()
method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
The time and space complexity of the sort cannot be guaranteed as it depends on the implementation.
Parameters
compareFunction Optional
Specifies a function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.
If
compareFunction(a, b)
returns a value > than 0, sortb
beforea
.If
compareFunction(a, b)
returns a value < than 0, sorta
beforeb
.If
compareFunction(a, b)
returns 0,a
andb
are considered equal.
Use arrow function expressions with shorter syntax
4. Push / Pop and Shift / Unshift
The push()
method adds one or more elements to the end of an array and returns the new length of the array.
The pop()
method removes the last element from an array and returns that element. This method changes the length of the array.
The shift()
method removes the first element from an array and returns that removed element. This method changes the length of the array.
The unshift()
method adds one or more elements to the beginning of an array and returns the new length of the array.
5. find
The find()
method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined
is returned.
Last updated