Array Methods Helpers
- Description:
Array Methods Helpers
Methods
(static) addItemToAnArray(arr, item) → {Array|undefined}
Add an item to the end of an array
.- Description:
Add an item to the end of an array
Example
addItemToAnArray([1, 2], 3) => [1, 2, 3]
addItemToAnArray(["a"], "b") => ["a", "b"]
addItemToAnArray([], { id: 1 }) => [{ id: 1 }]
addItemToAnArray("not an array", 1) => undefined
addItemToAnArray([1, 2], true) => [1, 2, true]
Parameters:
| Name | Type | Description |
|---|---|---|
arr |
Array | The array to which the item will be added |
item |
Number | Object | String | Boolean | The item to add to the array |
Returns:
Returns the updated array, or undefined if input is invalid
- Type
- Array | undefined
(static) addItemsInFrontToAnArray(arr, items) → {Array|undefined}
Add one or more items to the beginning of an array
.- Description:
Add one or more items to the beginning of an array
Example
addItemsInFrontToAnArray([3, 4], 1, 2) => [1, 2, 3, 4]
addItemsInFrontToAnArray(["b", "c"], "a") => ["a", "b", "c"]
addItemsInFrontToAnArray([], 1, 2) => [1, 2]
addItemsInFrontToAnArray("not an array", 1) => undefined
Parameters:
| Name | Type | Description |
|---|---|---|
arr |
Array | The array to which items will be added |
items |
any | One or more items to add at the beginning of the array |
Returns:
Returns the updated array, or undefined if input is not an array
- Type
- Array | undefined
(static) addOrRemoveItemsByIndex(arr, startIndex, count, items) → {Array|undefined}
Add or remove items from an array by index
.- Description:
Add or remove items from an array by index
Example
addOrRemoveItemsByIndex([1, 2, 3, 4], 1, 2) => [1, 4]
addOrRemoveItemsByIndex([1, 2, 3], 1, 1, "a", "b") => [1, "a", "b", 3]
addOrRemoveItemsByIndex([1, 2, 3], 1, 0, "a") => [1, "a", 2, 3]
addOrRemoveItemsByIndex("not an array", 1, 1) => undefined
Parameters:
| Name | Type | Description |
|---|---|---|
arr |
Array | The array to modify |
startIndex |
Number | The index at which to start changing the array |
count |
Number | Number of items to remove |
items |
any | Items to add at the given index |
Returns:
Returns the updated array, or undefined if input is invalid
- Type
- Array | undefined
(static) checkIsAllMatched(arr, condition, callbackopt) → {Boolean|undefined}
Check if all items in an array match a condition or callback
.- Description:
Check if all items in an array match a condition or callback
Example
checkIsAllMatched([2, 2, 2], 2) => true
checkIsAllMatched([{a:1}, {a:1}], {a:1}) => true
checkIsAllMatched([1, 2, 3, 4], null, (item) => item > 0) => true
checkIsAllMatched([1, 2, 3], 2) => false
checkIsAllMatched("not an array", 1) => undefined
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
arr |
Array | The array to check |
|
condition |
any | Object | Primitive value or object key-value to match |
|
callback |
function |
<optional> |
Optional function to decide if an item matches (must return boolean) |
Returns:
Returns true if all items match, false if not, undefined if input invalid
- Type
- Boolean | undefined
(static) checkIsMatched(arr, condition, callbackopt) → {Boolean|undefined}
Check if an array has at least one item matching a condition or callback
.- Description:
Check if an array has at least one item matching a condition or callback
Example
checkIsMatched([1, 2, 3], 2) => true
checkIsMatched([{a:1}, {a:2}], {a:1}) => true
checkIsMatched([1, 2, 3, 4], null, (item) => item % 2 === 0) => true
checkIsMatched([1, 2, 3], 4) => false
checkIsMatched("not an array", 1) => undefined
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
arr |
Array | The array to search |
|
condition |
any | Object | Primitive value or object key-value to match |
|
callback |
function |
<optional> |
Optional function that decides if an item matches (must return boolean) |
Returns:
Returns true if at least one item matches, false if none match, undefined if input invalid
- Type
- Boolean | undefined
(static) countItem(arr, keyOfCountopt, initialValueopt) → {Number|undefined}
Count or sum items in an array, optionally by key for objects
.- Description:
Count or sum items in an array, optionally by key for objects
Example
countItem([1, 2, 3]) => 6
countItem([{a:1}, {a:2}, {a:3}], "a") => 6
countItem([{a:1}, {a:2}, {b:3}], "a") => 3
countItem("not an array") => undefined
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
arr |
Array | The array to process |
||
keyOfCount |
String |
<optional> |
Optional key to sum values from objects |
|
initialValue |
Number |
<optional> |
0
|
Optional starting value |
Returns:
Returns the total count/sum, or undefined if input invalid
- Type
- Number | undefined
(static) filterAnArray(arr, condition, callbackopt) → {Array|undefined}
Filter an array by a condition (primitive or object key-value) with optional callback
.- Description:
Filter an array by a condition (primitive or object key-value) with optional callback
Example
filterAnArray([1, 2, 3, 2], 2) => [2, 2]
filterAnArray([{a:1}, {a:2}], {a:1}) => [{a:1}]
filterAnArray([1, 2, 3, 4], null, (item) => item % 2 === 0) => [2, 4]
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
arr |
Array | The array to filter |
|
condition |
any | Object | Value to filter by, or object with key-value pairs |
|
callback |
function |
<optional> |
Optional function to decide if an item should be kept (must return boolean) |
Returns:
Returns filtered array, or undefined if input is invalid
- Type
- Array | undefined
(static) findArrayByItem(arr, condition, callbackopt) → {any|undefined}
Find the first item in an array that matches a condition or callback
.- Description:
Find the first item in an array that matches a condition or callback
Example
findArrayByItem([1, 2, 3], 2) => 2
findArrayByItem([{a:1}, {a:2}], {a:1}) => {a:1}
findArrayByItem([1, 2, 3, 4], null, (item) => item % 2 === 0) => 2
findArrayByItem([1, 2, 3], 4) => undefined
findArrayByItem("not an array", 1) => undefined
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
arr |
Array | The array to search |
|
condition |
any | Object | Primitive value or object key-value to match |
|
callback |
function |
<optional> |
Optional function to decide if an item matches (must return boolean) |
Returns:
Returns the first matched item, or undefined if none found or input invalid
- Type
- any | undefined
(static) findIndexArrayItem(arr, condition, callbackopt) → {Number|undefined}
Find the index of the first item in an array that matches a condition or callback
.- Description:
Find the index of the first item in an array that matches a condition or callback
Example
findIndexArrayItem([1, 2, 3], 2) => 1
findIndexArrayItem([{a:1}, {a:2}], {a:2}) => 1
findIndexArrayItem([1, 2, 3, 4], null, (item) => item % 2 === 0) => 1
findIndexArrayItem([1, 2, 3], 4) => -1
findIndexArrayItem("not an array", 1) => undefined
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
arr |
Array | The array to search |
|
condition |
any | Object | Primitive value or object key-value to match |
|
callback |
function |
<optional> |
Optional function to decide if an item matches (must return boolean) |
Returns:
Returns the index of the first matched item, -1 if not found, or undefined if input invalid
- Type
- Number | undefined
(static) flatAnArray(arr, depthopt) → {Array|undefined}
Flatten an array up to a specified depth
.- Description:
Flatten an array up to a specified depth
Example
flatAnArray([1, [2, 3], [4, [5]]]) => [1, 2, 3, 4, [5]]
flatAnArray([1, [2, [3, 4]]], 2) => [1, 2, 3, 4]
flatAnArray("not an array") => undefined
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
arr |
Array | The array to flatten |
||
depth |
Number |
<optional> |
1
|
Depth level specifying how deep a nested array should be flattened |
Returns:
Returns a new flattened array, or undefined if input invalid
- Type
- Array | undefined
(static) getArrayLength(arr) → {Number|undefined}
Check the array Length
.- Description:
Check the array Length
Example
getArrayLength([1, 2]) => 2
getArrayLength([]) => 0
getArrayLength({}) => undefined
getArrayLength("hello") => undefined
getArrayLength(123) => undefined
getArrayLength(true) => undefined
getArrayLength(Infinity) => undefined
getArrayLength(NaN) => undefined
getArrayLength(() => {}) => undefined
getArrayLength(null) => undefined
Parameters:
| Name | Type | Description |
|---|---|---|
arr |
Array | To get the length of the array |
Returns:
Returns the Array lenght number, undefined if user input is not an array
- Type
- Number | undefined
(static) getItemByIndex(userArray, index) → {any|undefined}
Get an item from an array by its index
.- Description:
Get an item from an array by its index
Example
getItemByIndex([1, 2, 3], 0) => 1
getItemByIndex([1, 2, 3], 2) => 3
getItemByIndex([1, 2, 3], -1) => 3
getItemByIndex([1, 2, 3], -2) => 2
getItemByIndex([1, 2, 3], 5) => undefined
getItemByIndex("not an array", 0) => undefined
getItemByIndex([1, 2, 3], "0") => undefined
Parameters:
| Name | Type | Description |
|---|---|---|
userArray |
Array | The array from which to get the item |
index |
Number | The index of the item (supports negative indexing) |
Returns:
Returns the item at the given index, or undefined if input is invalid or index is out of bounds
- Type
- any | undefined
(static) mapAnArray(arr, callbackopt) → {Array|undefined}
Map over an array using a callback, or return original items if no callback is provided
.- Description:
Map over an array using a callback, or return original items if no callback is provided
Example
mapAnArray([1, 2, 3], x => x * 2) => [2, 4, 6]
mapAnArray(["a", "b"], (item, index) => item + index) => ["a0", "b1"]
mapAnArray([1, 2, 3]) => [1, 2, 3] // callback not provided
mapAnArray("not an array", x => x) => undefined
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
arr |
Array | The array to map |
|
callback |
function |
<optional> |
Optional function applied to each element (receives item and index) |
Returns:
Returns a new mapped array, or undefined if input is invalid
- Type
- Array | undefined
(static) merginMultipleArray() → {Array|undefined}
Merge multiple arrays into a single array
.- Description:
Merge multiple arrays into a single array
Example
merginMultipleArray([1, 2], [3, 4]) => [1, 2, 3, 4]
merginMultipleArray(["a"], ["b", "c"], ["d"]) => ["a", "b", "c", "d"]
merginMultipleArray([1], "not an array") => undefined
merginMultipleArray([], [true, false]) => [true, false]
Parameters:
| Name | Type | Description |
|---|---|---|
...arrs |
Array | One or more arrays to merge |
Returns:
Returns a single merged array, or undefined if any input is not an array
- Type
- Array | undefined
(static) removeFirstItemOfAnArray(arr) → {Array|undefined}
Remove the first item from an array
.- Description:
Remove the first item from an array
Example
removeFirstItemOfAnArray([1, 2, 3]) => [2, 3]
removeFirstItemOfAnArray(["a", "b"]) => ["b"]
removeFirstItemOfAnArray([]) => []
removeFirstItemOfAnArray("not an array") => undefined
Parameters:
| Name | Type | Description |
|---|---|---|
arr |
Array | The array from which the first item will be removed |
Returns:
Returns the updated array, or undefined if input is not an array
- Type
- Array | undefined
(static) removeLastItemOfAnArray(arr) → {Array|undefined}
Remove the last item from an array
.- Description:
Remove the last item from an array
Example
removeLastItemOfAnArray([1, 2, 3]) => [1, 2]
removeLastItemOfAnArray(["a", "b"]) => ["a"]
removeLastItemOfAnArray([]) => []
removeLastItemOfAnArray("not an array") => undefined
Parameters:
| Name | Type | Description |
|---|---|---|
arr |
Array | The array from which the last item will be removed |
Returns:
Returns the updated array, or undefined if input is not an array
- Type
- Array | undefined
(static) sortAnArray(arr, sortByopt) → {Array|undefined}
Sort an array generically or by object key
.- Description:
Sort an array generically or by object key
Example
sortAnArray([3, 1, 2]) => [1, 2, 3]
sortAnArray(["b", "a", "c"]) => ["a", "b", "c"]
sortAnArray([{ age: 30 }, { age: 20 }], "age") => [{ age: 20 }, { age: 30 }]
sortAnArray("not an array") => undefined
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
arr |
Array | The array to sort |
|
sortBy |
String |
<optional> |
Optional key to sort objects by |
Returns:
Returns the sorted array, or undefined if input is invalid
- Type
- Array | undefined