Returns true if an array contains a given value.
This is just a convenient shorthand to prevent having to use indexOf(...) > -1
, and does not currently check for object equality.
Usage:
Arr.contains("apple", ["grape", "apple", "banana"]) // true
Arr.contains(4, [1, 2, 3]) // false
Given an array and a field or function determining a unique string, returns a new distinct array.
If 2 items are different, but are determined to be unique base on the distinguishing field or function, then the first item in the array is chosen.
Usage:
const fruits = [
{ name: "apple", color: "red" },
{ name: "apple", color: "green" },
{ name: "grape", color: "purple" },
{ name: "apple", color: "red" }, // Duplicate
{ name: "grape", color: "green" }
]
Arr.distinctBy("name", fruits)
// [{ name: "apple", color: "red" },{ name: "grape", color: "purple" },]
Arr.distinctBy((fruit: any) => fruit.color, fruits)
// [{ name: "apple", color: "red" },{ name: "apple", color: "green" },{ name: "grape", color: "purple" }]
Returns the first item in an array, or null
if the array is empty.
If the argument is not an array, returns null
.
Can pass in a number N and it will return first N items, or entire array if array length less than N.
Usage:
Arr.first([1, 2, 3]) // 1
Arr.first([]) // null
Arr.first(2, [1, 2, 3, 4, 5]) // [1, 2]
Returns true
if a value is an array and is empty.
Usage:
Arr.isEmptyArray([]) // true
Arr.isEmptyArray([1]) // false
Arr.isEmptyArray(null) // false; it is not an array so false by default
Returns the last item in an array, or null
if the array is empty.
If the argument is not an array, returns null
.
Can pass in a number N and it will return the last N items, or entire array if array length less than N.
Usage:
Arr.last([1, 2, 3]) // 3
Arr.last([]) // null
Arr.last(2, [1, 2, 3, 4, 5]) // [4, 5]
Returns the last item in an array, or null
if the array is empty.
If the argument is not an array, returns null
.
Usage:
Arr.lastItem([1, 2, 3]) // 3
Arr.lastItem([]) // null
This module contains methods for dealing with arrays.