Array utils.
- Source:
Methods
(static) arrayMax(array) → {Number}
- Source:
- Since:
- 0.0.5
Gets the max value in an array of numbers.
Example
arrayMax([4, 5, 2, 3, 1]) // => 5
Parameters:
Name | Type | Description |
---|---|---|
array |
Array.<Number> |
Returns:
- Type
- Number
(static) arrayMaxByKey(key) → {function}
- Source:
- Since:
- 0.0.5
Gets the max numeric value of the given key in an array of objects.
Example
const scores = [
{ score: 7, user: "John" },
{ score: 9, user: "Jane" },
{ score: 5, user: "Mario" }
];
const getMaxScore = arrayMaxByKey("score");
getMaxScore(scores) // => 9
Parameters:
Name | Type | Description |
---|---|---|
key |
String |
Returns:
Object[] => Number
- Type
- function
(static) arrayMin(array) → {Number}
- Source:
- Since:
- 0.0.5
Gets the min value in an array of numbers.
Example
arrayMin([4, 5, 2, 3, 1]) // => 1
Parameters:
Name | Type | Description |
---|---|---|
array |
Array.<Number> |
Returns:
- Type
- Number
(static) arrayMinByKey(key) → {function}
- Source:
- Since:
- 0.0.5
Gets the min numeric value of the given key in an array of objects.
Example
const scores = [
{ score: 7, user: "John" },
{ score: 9, user: "Jane" },
{ score: 5, user: "Mario" }
];
const getMinScore = arrayMinByKey("score");
getMinScore(scores) // => 5
Parameters:
Name | Type | Description |
---|---|---|
key |
String |
Returns:
Object[] => Number
- Type
- function
(static) arraySum(source) → {Number}
- Source:
- Since:
- 0.0.5
Sums the values in the given array.
Example
arraySum([1, 2, 3, 4, 5]) // => 15
Parameters:
Name | Type | Description |
---|---|---|
source |
Array.<Number> |
Throws:
-
If the received array is empty.
- Type
- TypeError
Returns:
- Type
- Number
(static) arraySumByKey(key) → {function}
- Source:
- Since:
- 0.0.5
Builds a function that sums the values in the given key in an array of objects.
The built function will throw a TypeError if the received array is empty.
Example
const scores = [
{ score: 7, user: "John" },
{ score: 9, user: "Jane" },
{ score: 5, user: "Mario" }
];
const sumScores = arraySumByKey("score");
sumScores(scores) // => 21
Parameters:
Name | Type | Description |
---|---|---|
key |
String |
Returns:
Object[] => Number
- Type
- function
(static) countByKey(key) → {function}
- Source:
- Since:
- 0.0.7
Transforms an array-like of objects in a lookup table having as keys the property values of the given key in the source objects and as values the count of matches for the key.
Example
const persons = [
{ name: "Jane", city: "New York" },
{ name: "John", city: "New York" },
{ name: "Mario", city: "Rome" },
{ name: "Paolo" }
];
const countByCity = countByKey("city");
countByCity(persons) // => { "New York": 2, "Rome": 1, "undefined": 1 }
Parameters:
Name | Type | Description |
---|---|---|
key |
String |
Returns:
ArrayLike => Object<String, Number>
- Type
- function
(static) countToMapBy(iteratee) → {function}
- Source:
- Since:
- 0.1.6
Uses the provided iteratee
to build a function returning aMap
with the generated keys and counts as values.
Like Lamb's countBy
but builds a function returning a Map
.
Useful to keep the type of the generated keys instead of having them converted to string.
Example
const persons = [
{ name: "Jane", city: "New York" },
{ name: "John", city: "New York" },
{ name: "Mario", city: "Rome" },
{ name: "Paolo" }
];
const countByCity = countToMapBy(({ city }) => String(city));
countByCity(persons) // => Map { "New York" → 2, "Rome" → 1, "undefined" → 1 }
Parameters:
Name | Type | Description |
---|---|---|
iteratee |
function | a Lamb ListIteratorCallback |
Returns:
Object[] => Map<Any, Number>
- Type
- function
(static) getMinAndMax() → {Array.<Number, Number>}
- Source:
- Since:
- 0.0.8
Gets the min and max values contained in an array of numbers.
Example
getMinAndMax([4, 5, 2, 3, 1]) // => [1, 5]
Parameters:
Type | Description |
---|---|
Array.<Number> |
Returns:
(static) getMinAndMaxFromKey(key) → {function}
- Source:
- Since:
- 0.0.5
Gets the min and max values contained in the given key in an array of objects.
Example
const scores = [
{ score: 7, user: "John" },
{ score: 9, user: "Jane" },
{ score: 5, user: "Mario" }
];
const getMinAndMaxScore = getMinAndMaxFromKey("score");
getMinAndMaxScore(scores) // => [5, 9]
Parameters:
Name | Type | Description |
---|---|---|
key |
String |
Returns:
Object[] => Array<Number, Number>
- Type
- function
(static) groupByKey(key) → {function}
- Source:
- Since:
- 0.0.5
Groups an array-like of objects by the given key.
Example
const persons = [
{ name: "Jane", city: "New York" },
{ name: "John", city: "New York" },
{ name: "Mario", city: "Rome" },
{ name: "Paolo" }
];
const groupByCity = groupByKey("city");
groupByCity(persons) // =>
// {
// "New York": [
// { "name": "Jane", "city": "New York" },
// { "name": "John", "city": "New York" }
// ],
// "Rome": [
// { "name": "Mario", "city": "Rome" }
// ],
// "undefined": [
// { "name": "Paolo" }
// ]
// }
Parameters:
Name | Type | Description |
---|---|---|
key |
String |
Returns:
Object[] => Object<String, Object[]>
- Type
- function
(static) indexByKey(key) → {function}
- Source:
- Since:
- 0.0.7
Indexes an array-like of objects by the given key.
Example
const users = [
{id: 1, name: "John"},
{id: 2, name: "Jane"},
{id: 3, name: "Mario"}
];
const indexByID = indexByKey("id");
indexById(users) // =>
// {
// "1": { id: 1, name: "John" },
// "2": { id: 2, name: "Jane" },
// "3": { id: 3, name: "Mario" }
// }
Parameters:
Name | Type | Description |
---|---|---|
key |
String |
Returns:
Object[] => Object<String, Object>
- Type
- function
(static) isContainedIn(arrayLike) → {function}
- Source:
- Since:
- 0.0.7
A left curried version of Lamb's isIn
.
Expects the array-like first, then builds a predicate
waiting for the value to look for.
Remember that equality tests are made with Lamb's areSVZ
which performs a "SameValueZero" comparison.
Example
const values = [2, 5, 3, -0, 1, 4];
const isInValues = isContainedIn(values);
isInValues(0) // => true
isInValues(-0) // => true
isInValues(3) // => true
isInValues(11) // => false
Parameters:
Name | Type | Description |
---|---|---|
arrayLike |
ArrayLike |
Returns:
Any => Boolean
- Type
- function
(static) select(keys) → {function}
- Source:
- Since:
- 0.0.2
Selects the desired keys from an array of objects; much like the SQL statement with the same name.
Example
const users = [
{id: 1, name: "Jane", surname: "Doe", active: false},
{id: 2, name: "John", surname: "Doe", active: true},
{id: 3, name: "Mario", surname: "Rossi", active: true},
{id: 4, name: "Paolo", surname: "Bianchi", active: false}
];
const selectUserInfo = select(["id", "active"]);
selectUserInfo(users) // =>
// [
// {id: 1, active: false},
// {id: 2, active: true},
// {id: 3, active: true},
// {id: 4, active: false}
// ]
Parameters:
Name | Type | Description |
---|---|---|
keys |
Array.<String> |
Returns:
Object[] => Object[]
- Type
- function
(static) sortBy(key) → {function}
- Source:
- Since:
- 0.0.2
Builds a function that performs an ascending sort using the values of the given key.
Example
const scores = [
{ score: 7, user: "John" },
{ score: 9, user: "Jane" },
{ score: 5, user: "Mario" }
];
const sortByScore = sortBy("score");
sortByScore(scores) // =>
// [
// { score: 5, user: "Mario" },
// { score: 7, user: "John" },
// { score: 9, user: "Jane" }
// ]
Parameters:
Name | Type | Description |
---|---|---|
key |
String |
Returns:
Object[] => Object[]
- Type
- function
(static) sortDescBy(key) → {function}
- Source:
- Since:
- 0.0.2
Builds a function that performs a descending sort using the values of the given key.
Example
const scores = [
{ score: 7, user: "John" },
{ score: 9, user: "Jane" },
{ score: 5, user: "Mario" }
];
const sortDescsByScore = sortDescBy("score");
sortDescsByScore(scores) // =>
// [
// { score: 9, user: "Jane" },
// { score: 7, user: "John" },
// { score: 5, user: "Mario" }
// ]
Parameters:
Name | Type | Description |
---|---|---|
key |
String |
Returns:
Object[] => Object[]
- Type
- function