Members
(inner, constant) intersect
- Description:
Creates and returns a new array of all items that exist in both passed in arrays
- Source:
Creates and returns a new array of all items that exist in both passed in arrays
Example
intersect([1], [1]) === [1]
Methods
(inner) areCountMapsEqual(mapA, mapB) → {Boolean}
- Description:
Returns true if the maps
- Source:
Parameters:
Name | Type | Description |
---|---|---|
mapA |
Map.<*, number> | |
mapB |
Map.<*, number> |
Returns:
- True if the item count it equal between mapA and mapB
- Type
- Boolean
(inner) areFrequencyEqual(arr, otherArr) → {Boolean}
- Description:
Checks if arrays are frequency equal. Does this by making only one pass over each array and using an auxillary map.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array.<*> | |
otherArr |
Array.<*> |
Returns:
- True if otherArr contains exactly the same elements as arr, where order does not matter, but frequency does
- Type
- Boolean
(inner) areSetEqual(arr, otherArr)
- Description:
Checks if arrays are set-equal: they contain the same elements, but element frequencies don't matter. Does this with one pass over each array and an auxilliary set.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array.<*> | |
otherArr |
Array.<*> |
(inner) buildElementCountMap(arr) → {Map.<*, number>}
- Description:
Builds a map of elements mapped to their frequency counts
- Source:
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array.<*> |
Returns:
- Type
- Map.<*, number>
(inner) cloneArr(arr) → {Array}
- Description:
Creates a copy of the passed in array.
Returns empty array, if param is not an array.
- Source:
Example
cloneArr([1,2,3])
// Returns copy of the passed on array
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | array to be copied |
Returns:
- copy of passed in array
- Type
- Array
(inner) eitherArr(a, b) → {*}
- Description:
Returns a if it is an Array, else returns b
- Source:
Example
const foo = eitherArr('hi', 1) // returns 1
const bar = eitherArr([ 2 ], 1) // returns [ 2 ]
Parameters:
Name | Type | Description |
---|---|---|
a |
* | |
b |
* |
Returns:
either a, if it's an array, or b
- Type
- *
(inner) ensureArr(val) → {Array}
- Description:
Ensures the passed in value is an array, else it returns it in an array
- Source:
Example
const foo = eitherArr('hi') // returns ['hi']
const bar = eitherArr([ 2 ]) // returns [ 2 ]
Parameters:
Name | Type | Description |
---|---|---|
val |
Array | * | Value to check if its an array |
Returns:
val if it's an array, or val in an array
- Type
- Array
(inner) findExtrema(arr, comparator) → {*}
- Description:
Finds the extremum (e.g. max, min) element within array
arr
as defined by thecomparator
function
- Source:
Example
const max = findExtrema([ { a: 1 }, { a: 2} ], (x, y) => x.a - y.a)
// max === { a: 2 }
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array.<*> | |
comparator |
function | comparison function like the compareFunction in sort: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort |
Returns:
the element in arr
that is the extremum as defined by comparator
. If arr is empty, this function returns null.
- Type
- *
(inner) findMax(arr, propSelectornullable)
- Description:
Returns the maximum element in arr
- Source:
Example
const items = [ { num: 1 }, { num: 3 } ]
findMax(items, item => item.num) // returns { num: 3 }
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
arr |
Array.<Object> | ||
propSelector |
function |
<nullable> |
optional property selector for choosing the property to compare with |
(inner) findMin(arr, propSelectornullable)
- Description:
Returns the minimum element in arr
- Source:
Example
const items = [ { num: 1 }, { num: 3 } ]
findMax(items, item => item.num) // returns { num: 1 }
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
arr |
Array.<Object> | ||
propSelector |
function |
<nullable> |
optional property selector for choosing the property to compare with |
(inner) flatArr(arr, optsopt) → {Array}
- Description:
Flattens an array to a single level
- Source:
Example
const arr = flatArr([[ 'flat', '' ], [ 'array' ]]) // returns ['flat', '', 'array']
const arrTruthy = flatArr([ 0, 2, [ false ] ], { truthy: true }) // returns [ 2 ]
const arrExist = flatArr([ 0, 2, [ false ] ], { exists: true }) // returns [ 0, 2, false ]
const mutateArr = [ [1], [2] ]
flatArr(mutateArr, { mutate: true }) === mutateArr
// Evaluates to true, but mutateArr value is [ 1, 2 ]
Parameters:
Name | Type | Attributes | Default | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arr |
Array | * | Array to be flattened |
||||||||||||||||||
opts |
Object |
<optional> |
{}
|
Options to modify how the array is flattened Properties
|
Returns:
- Mutated original array now flattened, or a new flattened array based on options
- Type
- Array
(inner) flatMap(arr, mapFn)
- Description:
Maps each element using mapping function
mapFn
, but returns the result as a flattened array. It is equivalent to map() followed by flattening to depth 1, but flatMap is a useful shortcut, and merging both steps into one method (with one pass over the array) is slightly more efficient.
- Source:
Example
[1, 2].map(x => [x * 2]) // returns [[2], [4]]
flatMap([1, 2], x => [x * 2]) // returns [2, 4]
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | array to map across |
mapFn |
function | function for mapping |
(inner) flatUnion(arr, selectornullable) → {Array}
- Description:
Flattens the passed in array arguments and removes duplicates Also removes non-existing values such as undefined and null If the last argument is a function, it will be used as the comparison when checking for duplicates
- Source:
Examples
flatUnion([1,1,2], [1,2,3])
// Returns array with only unique values [ 1, 2, 3 ]
flatUnion([{a: 1}, { a: 3 }], [{a: 4}, { a: 1 }], item => item.a)
// Returns array with only unique values [ { a: 1 }, { a: 3 }, { a: 4 } ]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
arr |
Array | array to remove duplicates from |
|
selector |
function |
<nullable> |
optional function to specify the property to check if another element exists |
Returns:
- Flattened copy of passed in array arguments, with duplicates removed
- Type
- Array
(inner) isArr(value) → {Boolean}
- Description:
Checks if passed in value is an array.
- Source:
Example
isArr([1,2,3])
// Returns true
Parameters:
Name | Type | Description |
---|---|---|
value |
any | value to be check if is an array |
Returns:
- T/F value is an array
- Type
- Boolean
(inner) omitRange(arr, startIndex, count)
- Description:
Returns a new array with the same elements as arr, excluding
count
elements beginning at indexstartIndex
- Source:
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | |
startIndex |
Number | |
count |
Number |
(inner) randomArr(arr, amountopt) → {Array}
- Description:
Randomly selects values from a passed in array.
- Source:
Example
randomArr([1,2,3], 1)
// Returns an array with one of the values in the passed in array
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
arr |
Array | array to select values from |
|
amount |
Number |
<optional> |
number of values to select from the array |
Returns:
- randomly sorted array
- Type
- Array
(inner) randomizeArr(arr) → {Array}
- Description:
Randomly sorts an arrays items.
- Source:
Example
randomizeArr([1,2,3])
// Returns an array randomly sorted
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | array to randomly sorted |
Returns:
- randomly sorted array
- Type
- Array
(inner) uniqArr(arr, selectoropt) → {array}
- Description:
Removes duplicates from an array.
- Source:
Examples
uniqArr([1,1,2,3,3])
// Returns array with only unique values [ 1, 2, 3 ]
uniqArr([ {a: 1} , { a: 1 }], element => element.a)
// Returns array [ { a: 1 } ]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
arr |
array | array to remove duplicates from |
|
selector |
function |
<optional> |
optional function to specify the property uniqArr should use to check if another element exists |
Returns:
copy of passed in array, with duplicates removed
- Type
- array
(inner) uniqArrByReference(arr) → {array}
- Description:
Removes duplicates from an array, checking by reference-equality
- Source:
Example
uniqArr([1,1,2,3,3])
// Returns array with only unique values [ 1, 2, 3 ]
Parameters:
Name | Type | Description |
---|---|---|
arr |
array | array to remove duplicates from |
Returns:
copy of passed in array, with duplicates removed
- Type
- array