Array Methods
import { array } from 'ajuda'
deepCopy
Description
Creates a deep copy of an array (where nested elements will not reference the original).
deepCopy(array: any[]): any[]
Parameters
{Array} array
An array of elements.
Returns
{Array}
A new array.
Example
let alpha = [1, [ 2 ]]
let bravo = array.deepCopy(alpha)
alpha[1][0] = 3
bravo[1][0] === 3
// false
bravo[1][0] === 2
// true
flatten
Description
Flattens an array. If no depth is specified, the array is flattened completely by default.
array.flatten(array: any[], number?: number): any[]
Parameters
{Array} array
An array of elements.
{number} number
The depth at which to flatten. If omitted, defaults to Infinity.
Returns
{Array}
A flattened array.
Example
array.flatten([1, 2, [3]])
// [1, 2, 3]
array.flatten([1, 2, [3], [[4,5]]], 2)
// [1, 2, 3, 4, 5]
hasDuplicates
Description
Check if an array has duplicate values.
array.hasDuplicates(array: any[]): boolean
Parameters
{Array} array
An array of elements.
Returns
{Boolean}
true
if the array has duplicates, false
otherwise.
Example
array.hasDuplicates([1, 2, 3, 3])
// true
array.hasDuplicates([1, 2, 3])
// false
isEqual
Description
Checks if two arrays are shallowly equal. That is, they have the same elements, in the same order, but occupy different addresses in memory.
array.isEqual(array: any[], _array: any[]): boolean
Parameters
{Array} array
The first array to be compared.
{Array} _array
The second array to be compared.
Returns
{boolean}
true
if the arrays are shallowly equal, false
otherwise.
Example
let arr1 = [1, 2, 44, 5]
let arr2 = [1, 2, 5, 44]
let arr3 = [1, 2, 44, 5]
array.isEqual(arr1, arr2)
// false
array.isEqual(arr1, arr3)
// true
removeDuplicates
Description
Removes duplicate elements in an array.
array.removeDuplicates(array: any[]): any[]
Parameters
{Array} array
An array of elements.
Returns
{Array}
An array without duplicate elements.
Example
array.removeDuplicates([1, 2, 3, 3])
// [1, 2, 3]
array.removeDuplicates([1, 2, 3, 3, 'alice', 'bob', 'alice'])
// [1, 2, 3, 'alice', 'bob']
sanitize
Description
Removes negative array indices, as well as hidden properties.
array.sanitize(array: any[]): any[]
Parameters
{Array} array
An array with possible negative indices and/or hidden properties.
Returns
{Array}
An array without negative indices or hidden properties.
Example
let example = [1, 2, 3, 'alice', 'bob']
example.foo = 'bar'
example[-1] = 69
example.length // 5
Object.values(example) // [1, 2, 3, 'alice', 'bob', 'bar', 69]
array.sanitize(example)
// [1, 2, 3, 'alice', 'bob']
sortNumbers
Description
Sorts numbers according to size.
array.sortNumbers(array: number[], { sort }?: { sort: 'asc' | 'desc' }): number[]
Parameters
{Array<number>} array
An array of unsorted numbers.
{ sort: 'asc' | 'desc' }
A sorting direction value either asc
or desc
(defaults to asc
if omitted or malformed).
Returns
{Array<number>}
An array of sorted numbers.
Example
let unsorted = [1, 5, 66, 3, 8, 7, 99, 33]
array.sortNumbers(unsorted, { sort: 'asc' })
// [1, 3, 5, 7, 9, 33, 66, 99]
array.sortNumbers(unsorted)
// [1, 3, 5, 7, 9, 33, 66, 99]
array.sortNumbers(unsorted, { sort: 'desc' })
// [99, 66, 33, 9, 7, 5, 3, 1]
sortStrings
Sorts an array of strings alphabetically.
array.sortStrings(array: string[], { sort }?: { sort: 'asc' | 'desc' }): string[]
Parameters
{Array<string>} array
An array of unsorted strings.
{ sort: 'asc' | 'desc' }
A sorting direction value either asc
or desc
(defaults to asc
if omitted or malformed).
Returns
{Array<string>}
An array of sorted strings.
Example
let unsorted = ['foo', 'c', 'aa', 'bar', 'bbb']
array.sortStrings(unsorted)
// ['aa', 'bar', 'bbb', 'c', 'foo']
array.sortStrings(unsorted, { sort: 'asc' })
// ['aa', 'bar', 'bbb', 'c', 'foo']
array.sortStrings(unsorted, { sort: 'desc' })
// ['foo', 'c', 'bbb', 'bar', 'aa']
sortStringsByLength
Sorts an array of strings by their length.
array.sortStringsByLength(array: string[]): string[]
Parameters
{Array<string>} array
An array of strings.
Returns
{Array<string>}
An array of sorted strings.
Example
let unsorted = ['aaa', 'b', 'cc', 'foo', 'bars']
array.sortStringsByLength(unsorted)
// ['b', 'cc', 'aaa', 'foo', 'bars']