- Source:
Utils targeted to array type
Methods
array2object(value, keys) → {Object}
- Source:
- Signature:
-
a -> [String] -> StrMap a
- Category:
-
- Transformation
Takes a value and an array of strings and merges them into a single object. The provided value will be the value of all the keys. This function may be useful when building things like projections, where you have a list of fields you want to include and the semantics require an object with those fields set to a value (ej 1 or true)
Example
// This is an example using mongodb projection semantics
const toProject = ['name','age']
const projectFields = array2object (1)
const projection = projectFields (toProject)
// => {name: 1, age: 1}
Parameters:
Name | Type | Description |
---|---|---|
value |
Any | The value that will be assigned to every key |
keys |
Array.<String> | An array of strings that will be used as keys |
Returns:
an object containing all the provided strings as keys with the provided value
- Type
- Object
chunks(size, arr) → {Array.<Array>}
- Source:
- Signature:
-
FiniteNumber -> [a] -> [[a]]
- Category:
-
- Transformation
Creates an array of elements split into groups the length of size. If array can't be split evenly, the last chunk will be the remaining elements. A chunk size bigger than the size of the array will just return the original array wrapped
Example
const elements = [1,2,3,4,5,6]
chunks (3) (elements)
// => [[1,2,3],[4,5,6]]
chunks (4) (elements)
// => [[1,2,3,4],[5,6]]
Parameters:
Name | Type | Description |
---|---|---|
size |
Number | the size of each chunk |
arr |
Array | an array you want to split into chunks |
Returns:
an array of arrays of the provided size
indexBy(key) → {Object}
- Source:
- Signature:
-
String -> [Object] -> Object
- Category:
-
- List
Example
const indexByName = indexBy ('name')
indexByName([{name:'joe', age: 12 },{name:'peter', age:15}])
// => {joe: {age: 12, name: 'joe'}, peter: {age: 15, name: 'peter'}}
Parameters:
Name | Type | Description |
---|---|---|
key |
String | String representing the key you want to index by |
Returns:
Hashmap containing all the objects of the array indexed by the key
property
- Type
- Object
push(arr, x) → {Array}
- Source:
- Category:
-
- List
push :: [a] -> a -> [a]
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | The array to use as base to push any element |
x |
Any | Any kind of item that will be pushed into the array |
Returns:
The array with a new element at the end
- Type
- Array