Array
Array utility class that provides various methods for arrays.
The Array class is extended from the built-in Array object. You can continue to use whatever we normally use.
Usage
To use this module, you first need to import the Array class:
import { Array } from '@ponsetya/core'
Static Methods
Array.compare()
The compare() method compares two arrays and checks if they are the same.
If two arrays are different in terms of their reference or number, they are compared using the built-in JSON object. If the given array contains non-primitive types like functions, they are converted to strings for comparison. To learn more about how this works in detail, please refer to the JSON object.
Array.compare<T>(arr1: T[], arr2: T[]): boolean
If you want to compare two different array types, the result is likely to be false
.
Parameters
- arr1:
T[]
- The first array to compare.
- arr2:
T[]
- The first second to compare.
- Returns:
boolean
- A boolean indicating if the arrays are the same.
Examples
const arr1 = [1, 2, 3, 4]
const arr2 = [1, 2, 3, 4]
console.log(Array.compare(arr1, arr2)) // true
For strings with different number of elements or the same references, the result is returned without comparison.
const arr1 = [1, 2, 3, 4]
const arr2 = [1, 2]
console.log(Array.compare(arr1, arr1)) // true
console.log(Array.compare(arr1, arr2)) // false
Array.insert()
The insert() method inserts one or more items into an array at a specified index.
Array.insert<T>(arr: T[], index: number, ...items: T[]): void
Parameters
- arr:
T[]
- The array to insert the items into.
- index:
number
- The index to insert the items at.
- ...items:
T[]
- The items to insert.
Examples
const arr = ['b', 'c', 'd']
Array.insert(arr, 0, 'a')
Array.insert(arr, 4, 'e', 'f')
console.log(arr) // ['a', 'b', 'c', 'd', 'e', 'f']
Array.is()
The is() method determines whether the passed value is an Array.
Array.is(value: unknown): boolean
Parameters
- value:
unkown
- The value to be checked.
- Returns:
boolean
- A boolean indicating whether the variable is a Array.
Examples
console.log(Array.is(['x', 'y'])) // true
console.log(Array.is([])) // true
console.log(Array.is({ x: true })) // false
console.log(Array.is({})) // false
Array.move()
The move() method moves an item from one index to another in an array.
Array.move<T>(arr: T[], from: number, to: number): void
Parameters
- arr:
T[]
- The array to move the item in.
- from:
number
- The index of the item to move.
- to:
number
- The index to move the item to.
Examples
const arr = [1, 4, 2, 3]
Array.move(arr, 1, 3)
console.log(arr) // [1, 2, 3, 4]
Array.remove()
The remove() method removes an item from an array by index.
Array.remove<T>(arr: T[], index: number): void
Parameters
- arr:
T[]
- The array to remove the item from.
- index:
number
- The index of the item to remove.
Examples
const arr = [1, 2, 9, 3, 4]
Array.remove(arr, 2)
console.log(arr) // [1, 2, 3, 4]
Array.unique()
The unique() method removes duplicate items from an array.
This method is only valid for array if it consists of some primitive type like string or number.
Array.unique<T>(arr: T[]): T[]
Parameters
- arr:
T[]
- The array to remove duplicates from.
- Returns:
T[]
- A new array without duplicates.
Examples
const arr = [1, 1, 2, 3, 4, 4]
console.log(Array.unique(arr)) // [1, 2, 3, 4]