Functions utils for arrays
- Source
Methods
(static) allEqual(arr) → {boolean}
Check if all Elements of the array are equal
Name | Type | Description |
---|---|---|
arr | any | the array to check all elements |
- Source
true if all elements of the array ara equal
- Type:
- boolean
allEqual([0,0,0,0]) // true
allEqual([0,0,0,1]) // false
allEqual([0,"a","a"]) // false
allEqual([[1,1],[1,1]]) // true
allEqual([[1,1],[1,0]]) // false
allEqual([{a:"b",c:1},{a:"b"}]) // false
allEqual([{a:"b",c:1},{a:"b",c:1}]) // true
allEqual([{a:"b",c:1},{a:"b",c:1},{a:"c",c:1}]) // false
allEqual([{a:"b",c:1},{a:"b",c:1},{a:"b",c:1}]) // true
(static) choice(array) → {any}
Returns a random item from the array
Name | Type | Description |
---|---|---|
array | Array.<any> | the array to select a random item |
- Source
- Type:
- any
choice(["A", "B", "C"]); // "B"
choice([10, 5, 123]); // 10
(static) chunk(array, size)
Splits an array into smaller arrays of a specified size.
Name | Type | Description |
---|---|---|
array | Array | the array to split into smaller arrays |
size | number | the number of elements to split |
- Source
an array of smaller arrays
chunk(["A", "B", "C", "D", "E", "F", "G"], 2); // [["A", "B"], ["C", "D"], ["E", "F"], ["G"]]
(static) findBigObject(array, prop, returnOnlyValueopt) → {any|number}
Returns the biggest element of a array of objects.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
array | Array.<any> | The array to search | ||
prop | string | The property to find the biggest element | ||
returnOnlyValue | boolean | <optional> | false | If true only returns the value of the given property with the biggest value |
- Source
- The biggest element in the array
- Type:
- any |
number
const myArray = [{a:1, b:100}, {a: 0, b:50}, {a:0, b:200}]
findBigObject(myArray, "b"); // {a:0, b:200}
findBigObject(myArray, "a"); // {a:1, b:100}
findBigObject(myArray, "b", true); // 200
findBigObject(myArray, "a", true); // 1
(static) findBigObjectDeprecated(array, prop) → {Object}
Name | Type | Description |
---|---|---|
array | Array.<Object> | The array to search |
prop | string | The property to find the biggest element |
- Deprecated
- Yes
- Source
- The biggest element in the array
- Type:
- Object
(static) findLowObject(array, prop, returnOnlyValueopt) → {Object}
Returns the lowest element from array of objects
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
array | Array.<Object> | The array to search | ||
prop | string | The property to find the lowest element | ||
returnOnlyValue | boolean | <optional> | false | If true only returns the value of the given property with the lowest value |
- Source
- The lowest element in the array
- Type:
- Object
const myArray = [{a:1, b:100}, {a:10, b:50}, {a:0, b:200}]
findLowObject(myArray, "b"); // {a:10, b:50}
findLowObject(myArray, "a"); // {a:0, b:200}
findLowObject(myArray, "b", true); // 50
findLowObject(myArray, "a", true); // 0
(static) findLowObjectDeprecated(array, prop) → {Object}
Name | Type | Description |
---|---|---|
array | Array.<Object> | The array to search |
prop | string | The property to find the lowest element |
- Deprecated
- Yes
- Source
- The lowest element in the array
- Type:
- Object
(static) isItemInCommon(arr1, arr2) → {boolean}
Checks if an item is in common between two arrays
- Source
true if the item is in common
- Type:
- boolean
isItemInCommon([1, 2, 3], [2, 3, 4]); // true
isItemInCommon(["A", "B", "C"], ["B", "C", "D"]) // true
isItemInCommon([1, 2, 3], [4, 5, 6]); // false
(static) isSorted(arr) → {boolean}
Check if the given array is sorted fom lowest to highest
Name | Type | Description |
---|---|---|
arr | any | the array to check |
- Source
true if the array is sorted
- Type:
- boolean
isSorted([]) // true
isSorted([0,0,0,0]) // true
isSorted([2,1,4]) // false
isSorted([3,2,1]) // false
isSorted([1,2,3]) // true
isSorted(["B","A","D"]) // false
isSorted(["A","B","C"]) // true
(static) moveLeft(array, times) → {Array.<any>}
Move an array element to the right
Name | Type | Description |
---|---|---|
array | Array.<any> | The array to move |
times | number | The number of times to move the array |
- Source
- Type:
- Array.<any>
moveLeft([1,2,3,4,5]); // [2,3,4,5,1]
moveLeft([1,2,3,4,5], 2); // [3,4,5,1,2]
moveLeft(["a","b","c","d","e"], 7) // ["c","d","e","a","b"]
moveLeft(["a","b"], 3) // ["b","a"]
(static) removeDuplicatesObj(array, property) → {Array}
Removes duplicates from an array of objects based on a specified property.
Name | Type | Description |
---|---|---|
array | Array | The array of objects. |
property | string | The property to check for duplicates. |
- A new array with duplicate objects removed.
- Type:
- Array
const array = [
{id:1,name:"John"},
{id:2,name:"Jane"},
{id:3,name:"John"},
{id:4,name:"Mike"},
{id:5,name:"Jane"}
];
removeDuplicates(array, "name"); // [{id:1,name:"John"}, {id:2,name:"Jane"}, {id:4,name:"Mike"}]
(static) shuffle(array, mutateOriginal) → {Array.<any>}
This function will randomize the position of the array items. (change the original array!)
To not mutate the original array pass in the mutateOriginal
argument false, and this function will return a new array with the original items in random positions. (not changing the original array)
Name | Type | Description |
---|---|---|
array | Array.<any> | the array with the items to randomize |
mutateOriginal | boolean | the array with the items to randomize |
- Source
- Type:
- Array.<any>
shuffle(["A", "B", "C"]); // ["B","A","C"]
shuffle([1,2,3,4,5,6,7,8,9]); // [8,5,1,4,3,6,9,2,7]
shuffle([{a:1},{b:2},{c:3}]); // [{a:1},{c:3},{b:2}]
(static) sortAscending(arr, mutateOriginalopt) → {Array.<number>}
Sort an array of number by ascending
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr | Array.<number> | the array to sort | ||
mutateOriginal | boolean | <optional> | false | if true will change the original array |
- Source
A new Array sorted
- Type:
- Array.<number>
const myArr = [10,4,2,7,1,0,11,4,2,3,5,8,4,3,0,6];
const myNewSortedArr = sortAscending(myArr); // [0,0,1,2,2,3,3,4,4,4,5,6,7,8,10,11]
(static) sortAscendingObject(arr, prop) → {Array.<any>}
Sort a array of objects in ascending order by property
Name | Type | Description |
---|---|---|
arr | Array.<any> | the array to sort |
prop | string | the property base to sort |
- a new sorted array by the given property
- Type:
- Array.<any>
(static) sortDescending(arr) → {Array.<number>}
Sort an array of number by descending
Name | Type | Description |
---|---|---|
arr | Array.<number> | the array to sort |
- Source
A new Array sorted
- Type:
- Array.<number>
const myArr = [10,4,2,7,1,0,11,4,2,3,5,8,4,3,0,6];
const myNewSortedArr = sortDescending(myArr); // [11,10,8,7,6,5,4,4,4,3,3,2,2,1,0,0]
(static) sortDescendingObject(arr, prop) → {Array.<any>}
Sort a array of objects in descending order by property
Name | Type | Description |
---|---|---|
arr | Array.<any> | the array to sort |
prop | string | the property base to sort |
- a new sorted array by the given property
- Type:
- Array.<any>