Methods to make working with arrays more convenient and collection-like.
- Source
Methods
(inner) batchify(array, constrainedFunc, context)
Takes elements and fits them into subarrays (= batches) so that for each batch constrainedFunc returns true. Note that contrainedFunc should at least produce 1-length batches, otherwise an error is raised.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
constrainedFunc | function | |
context | Object | bound to |
- Source
(inner) binarySearchFor(array, searchValue, converter, returnClosestElement) → {Object}
Returns the element equal to the given search value or undefined.
If defined, a converter function will be applied to compare an array element with the search value
If returnClosestElement is true, the element closest to the search value will be returned, even if it is not equal.
If false, only an exact match will be returned, otherwise undefined will be returned.
| Name | Type | Default | Description |
|---|---|---|---|
array | Array.<any> | The array in which to search | |
searchValue | Object | The value to search for in | |
converter | function | The function used to compare array elements with | |
returnClosestElement | Boolean | false | Whether only exact matches should be returned |
- Source
- Type:
- Object
(inner) clear(array)
Removes all items. Mutating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> |
- Source
(inner) clone(array)
Returns a shallow copy of array.
| Name | Type | Description |
|---|---|---|
array | Array.<any> |
- Source
(inner) combinations(listOfListsOfValues) → {Array.<Array.<any>>}
Given a "listOfListsOfValues" in the form of an array of arrays, retrieve all the combinations by picking one item from each array. This basically creates a search tree, traverses it and gathers all node values whenever a leaf node is reached.
| Name | Type | Description |
|---|---|---|
listOfListsOfValues | Array.<Array.<any>> |
- Source
- Type:
- Array.<Array.<any>>
(inner) combinationsPick(listOfListsOfValues, pickIndices)
Can be used to recursively create all combinations of elements in n arrays. Given a "listOfListsOfValues" in the form of an array of arrays and pickIndices list with the size of the number of arrays which indicates what values to pick from each of the arrays. Returns a list with two lists:
- values picked from each of the arrays, 2. the next pickIndices or null if at end (no more combinations possible). Needs to be called recursively to enumerate alls combinations.
// Example:
var searchSpace = [["a", "b", "c"], [1,2]];
arr.combinationsPick(searchSpace, [0,1]);
// => [["a",2], [1,0]]
arr.combinationsPick(searchSpace, [1,0]);
// => [["b",1], [1,1]]
| Name | Type | Description |
|---|---|---|
listOfListsOfValues | Array.<Array.<any>> | |
pickIndices | Array.<number> |
- Source
(inner) compact(array) → {Array.<any>}
Returns a copy of array with falsy values removed.
| Name | Type | Description |
|---|---|---|
array | Array.<any> |
- Source
- Type:
- Array.<any>
(inner) count(array, item)
Returns the number of times item occurs in array.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
item | any |
- Source
(inner) deepEquals(array, otherArray, m) → {boolean}
Returns true if each element in array is structurally equal (lang.obj.equals) to its corresponding element in otherArray.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
otherArray | Array.<any> | |
m | Array.<any> | Gets populated with memorization when this function is called as part of |
- Source
- Type:
- boolean
(inner) doAndContinue(array, iterator, endFunc, context)
Iterates over array but instead of consecutively calling iterator, iterator gets passed in the invocation for the next iteration step as a function as first parameter. This allows to wait arbitrarily between operation steps, great for managing dependencies between tasks.
arr.doAndContinue([1,2,3,4], function(next, n) {
alert("At " + n);
setTimeout(next, 100);
}, function() { alert("Done"); })
If the elements are functions you can leave out the iterator:
arr.doAndContinue([
function(next) { alert("At " + 1); next(); },
function(next) { alert("At " + 2); next(); }
], null, function() { alert("Done"); });
| Name | Type | Description |
|---|---|---|
array | Array.<any> | The array to iterate over |
iterator | function | The function that is called for each element of |
endFunc | function | A function called once after iterating over |
context | Object | Bound to |
- Source
(inner) drop(arr, n) → {Array.<any>}
| Name | Type | Description |
|---|---|---|
arr | Array.<any> | |
n | number |
- Source
arr without the first n elements.
- Type:
- Array.<any>
(inner) dropWhile(arr, fun, context)
Return elements from arr in a list starting from fun being falsy while iterating over arr.
| Name | Type | Description |
|---|---|---|
arr | Array.<any> | |
fun | function | |
context | Object | bound to |
- Source
(inner) equals(array, otherArray) → {boolean}
Returns true if each element in array is equal (==) to its corresponding element in otherArray.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
otherArray | Array.<any> |
- Source
- Type:
- boolean
(inner) filterByKey(arr, key) → {Array.<any>}
Returns only the Objects in arr that have key as property.
| Name | Type | Description |
|---|---|---|
arr | Array.<any> | |
key | string |
- Source
- Type:
- Array.<any>
(inner) findAndGet(arr, iterator) → {Object}
Find the first occurence for which iterator returns a truthy value and return this value, i.e. unlike find the iterator result and not the element of the list is returned.
| Name | Type | Description |
|---|---|---|
arr | Array.<any> | |
iterator | function |
- Source
- Type:
- Object
(inner) first()
Returns the first element of an array.
- Source
(inner) genN(n, generator) → {Array.<any>}
Creates an array with the result of generator called n times.
arr.genN(3, num.random) => [46,77,95]
| Name | Type | Description |
|---|---|---|
n | number | |
generator | function |
- Source
- Type:
- Array.<any>
(inner) grep(arr, test) → {Array.<any>}
Returns an array that contains all elements of arr that contain/satisfy test. grep stringifies all elements in arr.
| Name | Type | Description |
|---|---|---|
arr | Array.<any> | |
test | String | |
- Source
- Type:
- Array.<any>
(inner) groupBy(array, iterator, context)
Applies iterator to each element in array and puts the return value into a collection associated to its stringified representation.
// Example: Groups characters by how often they occur in a string
var chars = arr.from("Hello World");
arr.groupBy(arr.uniq(chars), function(c) {
return arr.count(chars, c); })
=> {
"1": ["H","e"," ","W","r","d"],
"2": ["o"],
"3": ["l"]
}
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
iterator | function | |
context | Object | Bound to |
- Source
- See
- lively.lang/Group
(inner) groupByKey(array, key)
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
key | string |
- Source
- See
- lively.lang/array~groupBy
(inner) histogram(data, binSpec)
When called without binspec, returns data partitioned into lists containing two elements each. binspec can be an array of n numbers. If given, histogram will create n-1 bins, using the provided values in binspec as threshholds. The n-1 bins will be returned as a list. T
| Name | Type | Description |
|---|---|---|
data | Array.<number> | The data to be partitioned |
binSpec | Array.<number> | The threshholds used to partition the data. |
- Source
(inner) interpose(array, delim) → {Array.<any>}
/** Returns a new array that contains an element of arra and delim alternating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
delim | any |
- Source
- Type:
- Array.<any>
(inner) intersect(array1, array2)
Rerturns the intersection of array1 and array2 according to set semantic.
| Name | Type | Description |
|---|---|---|
array1 | Array.<any> | |
array2 | Array.<any> |
- Source
(inner) invoke(array, method, arg1, arg2, arg3, arg4, arg5, arg6)
Calls method on each element in array, passing all arguments. Often a handy way to avoid verbose map calls.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
method | function | The method to invoke on all elements of |
arg1 | any | |
arg2 | any | |
arg3 | any | |
arg4 | any | |
arg5 | any | |
arg6 | any |
- Source
(inner) isSorted(array, descending) → {boolean}
Returns a boolean indicating whether or not array is sorted.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
descending | boolean | indicating if |
- Source
wether array is sorted or not
- Type:
- boolean
(inner) isSubset(list1, list2) → {boolean}
Returns wether all elements in list1 are in list2.
| Name | Type | Description |
|---|---|---|
list1 | Array.<any> | |
list2 | Array.<any> |
- Source
- Type:
- boolean
(inner) last()
Returns the last element of an array.
- Source
(inner) mask(array, mask) → {Array.<any>}
Return an array containing all elements or array for which mask contains a truthy value at the same index.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | The array which should be subsetted. |
mask | Array.<Boolean> | Array used for masking |
- Source
- Type:
- Array.<any>
(inner) max(array, iterator, context)
Return the element of array which has the highest value according to iterator.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
iterator | function | |
context | Object | bound to |
- Source
(inner) min(array, iterator, context)
Return the element of array which has the smallest value according to iterator.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
iterator | function | |
context | Object | bound to |
- Source
(inner) mutableCompact(array) → {Array.<any>}
Returns array with falsy values removed.
| Name | Type | Description |
|---|---|---|
array | Array.<any> |
- Source
- Type:
- Array.<any>
(inner) nestedDelay(array, iterator, waitSecs, endFunc, context, optSynchronChunks)
Calls iterator for every element in array and waits between iterator calls waitSecs. Eventually endFunc is called. When passing a number n as optSynchronChunks, only every nth iteration is delayed.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | The array to iterate |
iterator | function | |
waitSecs | number | The number of seconds to wait between invocations of |
endFunc | function | A function called once after iterating over |
context | Object | Bound to |
optSynchronChunks | number | Only wait after each |
- Source
(inner) partition(array, iterator, context)
Partition array according to a condition specified in iterator. Creates two partitions (condition is either true or false).
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
iterator | function | |
context | Object | bound to |
- Source
(inner) permutations(array)
Returns the number of permutations of the elements in array.
| Name | Type | Description |
|---|---|---|
array | Array.<any> |
- Source
number
(inner) pluck(array, property)
Returns property or undefined from each element of array. For quick maps and similar to invoke.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
property | string | The property to return |
- Source
(inner) pushAll(array, items) → {Array.<any>}
Appends all items to array. Mutating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
items | Array.<any> |
- Source
array
- Type:
- Array.<any>
(inner) pushAllAt(array, items, idx)
Inserts all items at idx. Mutating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
items | Array.<anu> | |
idx | number |
- Source
(inner) pushAt(array, item, index)
Inserts item at index. Mutating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
item | any | |
index | number |
- Source
(inner) pushIfNotIncluded(array, item)
Only appends item if its not already in array. Mutating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
item | any |
- Source
(inner) range(begin, end, step) → {Array.<number>}
Creates an array containing elements from begin until end with step-sized steps.
| Name | Type | Description |
|---|---|---|
begin | number | First element |
end | number | Last element |
step | number | step size |
- Source
- Type:
- Array.<number>
(inner) reject(array, func, context) → {Array.<any>}
Returns an array of all elements of array for which func is falsy.
| Name | Type | Description |
|---|---|---|
array | Array.<Obejct> | The array which should be subsetted. |
func | function | Function that is used for testing. |
context | Object | Acts as |
- Source
- Type:
- Array.<any>
(inner) rejectByKey(array, key) → {Array.<any>}
Returns an array of all elements of array that do not have key as property.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
key | string |
- Source
- Type:
- Array.<any>
(inner) reMatches(arr, re, stringifier)
Returns the matches of re for the elements in arr. Might include null items if re did not match.
| Name | Type | Description |
|---|---|---|
arr | Array.<any> | |
re | * | The refular expression to use |
stringifier | * | Used to stringify the elements of |
- Source
(inner) remove(array, item) → {any}
Removes the first occurrence of item in array. Mutating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
item | any |
- Source
item
- Type:
- any
(inner) removeAt(array, index)
Remove the element of array at index. Mutating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
index | number |
- Source
(inner) replaceAt(array, item, index)
Replace the element array[index] with item. Mutating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
item | any | |
index | number |
- Source
(inner) rotate(array, times) → {Array.<any>}
Shift the elements in array to the left times times.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
times | number |
- Source
- A copy of
arrayrotatedtimestimes
- Type:
- Array.<any>
(inner) shuffle(array)
Randomizes the order of elements in array. Non-mutating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> |
- Source
(inner) sortBy(array, iterator, context)
Sorts array according to the elements value of iterator.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | the array to sort |
iterator | function | the function to use to sort |
context | Object | Used as |
- Source
(inner) sortByKey(array, key)
Sorts array by the values of a given key.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | the array to sort |
key | string | the key to sort by |
- Source
(inner) sum(array)
Return the sum of the elements of array.
| Name | Type | Description |
|---|---|---|
array | Array.<number> |
- Source
(inner) swap(array, index1, index2)
Swap the element at array[index1] with the one at array[index2]. Mutating.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
index1 | number | |
index2 | number |
- Source
(inner) take(arr, n) → {Array.<any>}
| Name | Type | Description |
|---|---|---|
arr | Array.<any> | |
n | number |
- Source
The first n elements of arr.
- Type:
- Array.<any>
(inner) takeWhile(arr, fun, context)
Return elements from arr in a list until fun is falsy while iterating over arr.
| Name | Type | Description |
|---|---|---|
arr | Array.<any> | |
fun | function | |
context | Object | bound to |
- Source
(inner) toTuples(array, tupleLength)
Decomposes array into sub-arrays with at most tupleLength elements.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
tupleLength | number | description |
- Source
(inner) union(array1, array2)
Rerturns the union of array1 and array2 according to set semantic.
| Name | Type | Description |
|---|---|---|
array1 | Array.<any> | |
array2 | Array.<any> |
- Source
(inner) uniq(array, sorted) → {Array.<any>}
Returns array without duplicates.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
sorted | boolean | Wether |
- Source
- Type:
- Array.<any>
(inner) uniqBy(array, comparator, context) → {Array.<any>}
Like arr.uniq but with custom equality comparator(a,b).
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
comparator | function | Function used to determine if a equals b |
context | Object | Used as |
- Source
- Type:
- Array.<any>
(inner) uniqByKey(array, key) → {Array.<any>}
Like arr.uniq but with equality based on array[index].key.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
key | string |
- Source
- Type:
- Array.<any>
(inner) withN(n, obj) → {Array.<any>}
Returns an array filled with obj for n times.
| Name | Type | Description |
|---|---|---|
n | number | Length of the array to create |
obj | Object | Object with which the array is to be filled |
- Source
- Type:
- Array.<any>
(inner) without(array, elem) → {Array.<any>}
Returns a copy of array without elem.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
elem | Object |
- Source
- Type:
- Array.<any>
(inner) withoutAll(array, otherArr) → {Array.<any>}
Returns a copy of array without all elements in otherArr.
| Name | Type | Description |
|---|---|---|
array | Array.<any> | |
otherArr | Array.<any> |
- Source
- Type:
- Array.<any>
(inner) zip(arguments) → {Array.<any>}
Takes any number of lists as arguments. Combines them elment-wise.
arr.zip([1,2,3], ["a", "b", "c"], ["A", "B"])
=> [[1,"a","A"],[2,"b","B"],[3,"c",undefined]]
| Name | Type | Description |
|---|---|---|
arguments | Any number of lists |
- Source
- Type:
- Array.<any>