Pure JavaScript array utility functions
npm install phane-tech-array-utilsnull, undefined, empty arrays, and invalid inputs.
bash
npm install phane-tech-array-utils
`
---
๐ Import
`js
import {
getArrayLength,
addItemToAnArray,
addItemsInFrontToAnArray,
addOrRemoveItemsByIndex,
removeFirstItemOfAnArray,
removeLastItemOfAnArray,
mapAnArray,
filterAnArray,
findArrayItem,
findIndexArrayItem,
checkIsMatched,
checkIsAllMatched,
sortAnArray,
flatAnArray,
merginMultipleArray,
countItem
} from "phane-tech-array-utils";
`
---
๐ API Reference
$3
Returns the length of an array.
`js
getArrayLength([1,2,3]); // 3
getArrayLength([]); // 0
`
---
$3
Adds an item to the end of the array.
`js
addItemToAnArray([1,2], 3); // [1,2,3]
`
---
$3
Adds items to the beginning of an array.
`js
addItemsInFrontToAnArray([2,3], 1); // [1,2,3]
`
---
$3
Adds or removes items using index.
`js
addOrRemoveItemsByIndex([1,2,3], 1, 1); // [1,3]
`
---
$3
Removes the last element of an array.
`js
removeLastItemOfAnArray([1,2,3]); // [1,2]
`
---
$3
Removes the first element of an array.
`js
removeFirstItemOfAnArray([1,2,3]); // [2,3]
`
---
$3
Safely maps array values.
`js
mapAnArray([1,2,3], n => n * 2); // [2,4,6]
`
---
$3
Filters array values.
`js
filterAnArray([1,2,3], 2); // [2]
filterAnArray([{a:1},{a:2}], {a:1}); // [{a:1}]
`
---
$3
Finds the first matched item.
`js
findArrayItem([1,2,3], 2); // 2
`
---
$3
Returns the index of the matched item.
`js
findIndexArrayItem([1,2,3], v => v === 2); // 1
`
---
$3
Checks if any item matches.
`js
checkIsMatched([1,2,3], 2); // true
`
---
$3
Checks if all items match.
`js
checkIsAllMatched([2,2,2], 2); // true
`
---
$3
Sorts an array or array of objects.
`js
sortAnArray([3,1,2]); // [1,2,3]
sortAnArray([{a:2},{a:1}], "a"); // [{a:1},{a:2}]
`
---
$3
Deep flattens nested arrays.
`js
flatAnArray([1,[],[2,[3]]]); // [1,2,3]
`
---
$3
Merges multiple arrays into one.
`js
merginMultipleArray([1],[2],[3]); // [1,2,3]
`
---
$3
Counts or sums array values.
`js
countItem([1,2,3]); // 6
countItem([{a:1},{a:2}], "a"); // 3
``