ArrayHelper
Helper with useful functions for handling Arrays
Functions
10
Functions
10Combines provided arrays of by aggregating their element values. For example the below code sums up numeric elements of the arrays:
ArrayHelper.aggregate(
[
[0, 1, 2, 33]
[10, 1, -1],
[100, 1, -1]
],
entry => entry || 0, // "|| 0" here to make it work for different array sizes
(aggregated, entry) => aggregated + entry, // aggregate by summing up
() => 0 //initial value is zero
);
// returns [111, 3, 0, 33] array
| Parameter | Type | Description |
|---|---|---|
arrays | Array[] | Arrays to combine |
getEntryValueFn | function | Function that extracts an array entry for aggregating. |
aggregatorFn | function | A function to execute for each element in the arrays. It's purpose is to
aggregate the element value to the corresponding entry of the resulting array.
The function's return value becomes the value of the |
aggregatorFn.aggregated | Object | Resulting array entry value. On the first call
|
aggregatorFn.entry | Object | Current entry to aggregate into |
aggregatorFn.arrayIndex | Number | Index of current array (in the provided |
aggregatorFn.entryIndex | Object[] | Index of the current entry. |
aggregationContext | Object | A shared object providing extra aggregation call context. |
getInitialValueFn | function | Function that returns an initial value for the combined array entries. |
aggregationContext | Object | Optional object that is passed to all of the above functions that can
be used for keeping some additional parameters used when aggregating.
Out of the box the object will contain |
Resulting array of combined values.
This method returns the index that a given item would be inserted into the
given (sorted) array. Note that the given item may or may not be in the
array. This method will return the index of where the item should be.
For example:
var array = [ 'A', 'D', 'G', 'K', 'O', 'R', 'X' ];
var index = ArrayHelper.binarySearch(array, 'E');
console.log('index: ' + index);
// logs "index: 2"
array.splice(index, 0, 'E');
console.log('array : ' + array.join(''));
// logs "array: ADEGKORX"
| Parameter | Type | Description |
|---|---|---|
array | Object[] | The array to search. |
item | Object | The item that you want to insert into the |
begin | Number | The first index in the |
end | Number | The index that marks the end of the range to consider. The item at this index is not considered. |
compareFn | function | The comparison function that matches the sort
order of the |
The index for the given item in the given array based on
the passed compareFn.
Returns an array of unique values and their occurrence count, in { value, count } format
| Parameter | Type | Description |
|---|---|---|
array | Array | Input array |
New array with unique items and counts in { value, count } format
Similar to Array.prototype.fill(), but constructs a new array with the specified item count and fills it with clones of the supplied item.
| Parameter | Type | Description |
|---|---|---|
count | Number | Number of entries to create |
itemOrArray | Object | Array | Item or array of items to clone (uses object spread to create shallow clone) |
fn | function | An optional function that is called for each item added, to allow processing |
A new populated array
Similar to the native Array.find() call, but this finds the last element in the array for which
the passed function returns a truthy value.
| Parameter | Type | Description |
|---|---|---|
array | Object[] | The array to find in. |
fn | function | The testing function. |
thisObj | Object | The scope ( |
Similar to Array.from()
this method creates an array from an iterable object. Where Array.from() accepts a mapper function as the
second argument, this method accepts a filter function as its second argument. If a mapper function is also
needed, it can be passed as the third argument. Unlike Array.from(), if this method is passed null, it will
return an empty array.
| Parameter | Type | Description |
|---|---|---|
iterable | Array | The iterable object to convert (must support |
filter | function | A function to apply to each item of the |
map | function | A function to apply to each item of the |
Pushes item on to the array if not already included
| Parameter | Type | Description |
|---|---|---|
array | Array | Array to push to |
items | Object | Item(s) to push if not already included |
Populates an array with the return value from fn.
| Parameter | Type | Description |
|---|---|---|
count | Number | Number of entries to create |
fn | function | A function that is called |
fn.index | Number | Current index in the array |
A new populated array
Remove one or more items from an array
| Parameter | Type | Description |
|---|---|---|
array | Array | Array to remove from |
items | Object | Object[] | Set | One or more items to remove, or one Set containing items to remove |
Returns true if any item was removed
Returns a new array with the unique items from the supplied array.
| Parameter | Type | Description |
|---|---|---|
array | Array | Input array |
New array with unique items