Lists
Functions over lists. None of them mutate their input: sort and reverse return new lists. Functions that take a lambda accept an implicit . lambda or an arrow lambda, and call it with the item and its index.
Every function can be called as f(x, ...) or as a method, x.f(...). In the signatures, T and U stand for any type, T[] is a list, ? marks an optional parameter, and (T, number) => boolean is a lambda parameter.
The examples use this context:
const context = { items: [{ name: "Pen", price: 2, qty: 3 }, { name: "Book", price: 12, qty: 1 }, { name: "Bag", price: 30, qty: 1 }] }| Function | Description |
|---|---|
map | Transforms each item. |
filter | Keeps the items for which the lambda is true. |
find | The first item for which the lambda is true, or null. |
findIndex | The position of the first item for which the lambda is true, or -1. |
some | Whether the lambda is true for at least one item. |
every | Whether the lambda is true for every item. |
none | Whether the lambda is false for every item. |
count | The number of items, or of items for which the lambda is true. |
flatMap | Transforms each item and flattens list results one level. |
reduce | Folds a list into one value: reduce(list, (acc, item) => ..., initial). Name both parameters; "." is not allowed, since the first one is the accumulator. |
sort | Sorts numbers, text, timestamps, or durations; pass "desc" to reverse. |
sortBy | Sorts by a key; pass "desc" to reverse. Nulls sort first ("asc") or last ("desc"). |
groupBy | Groups items into a map of lists by a key. Map keys are text, so a key is its text form: 1 and "1", or true and "true", share a group. A key must be a string, number, or boolean (null is an error). Keys are listed integer-like first, ascending, then in first-seen order. |
reverse | The items in reverse order. |
unique | The items without duplicates (by value), in first-seen order. |
flat | Flattens nested lists one level. |
first | The first item, or null. |
last | The last item, or null. |
join | Joins items into text with a separator (default ","). |
slice | A section of text or a list; negative positions count from the end. |
at | The item or character at a position; negative positions count from the end. |
includes | Whether text contains a substring, or a list contains a value. |
indexOf | Position of the first match, or -1. |
lastIndexOf | Position of the last match, or -1. |
isEmpty | Whether a list, text, or map has no items; null is empty. |
min | The smallest value (nulls are skipped); null for an empty list. |
max | The largest value (nulls are skipped); null for an empty list. |
sum | The sum of the numbers in a list (nulls are skipped), added left to right as a + b + c is. |
avg | The mean of the numbers or durations in a list (nulls are skipped, summed as sum does); null for an empty list. |
map
Transforms each item.
map(T[], (T, number) => U): U[]
items.map(.name) // => ["Pen", "Book", "Bag"]
items.map((it, i) => `${i + 1}. ${it.name}`) // => ["1. Pen", "2. Book", "3. Bag"]filter
Keeps the items for which the lambda is true.
filter(T[], (T, number) => boolean): T[]
items.filter(.price > 5).map(.name) // => ["Book", "Bag"]find
The first item for which the lambda is true, or null.
find(T[], (T, number) => boolean): T | null
items.find(.price > 10) // => { name: "Book", price: 12, qty: 1 }
items.find(.price > 100) // => nullfindIndex
The position of the first item for which the lambda is true, or -1.
findIndex(T[], (T, number) => boolean): number
items.findIndex(.name == "Bag") // => 2some
Whether the lambda is true for at least one item.
some(T[], (T, number) => boolean): boolean
items.some(.price > 20) // => trueevery
Whether the lambda is true for every item.
every(T[], (T, number) => boolean): boolean
items.every(.qty >= 1) // => truenone
Whether the lambda is false for every item.
none(T[], (T, number) => boolean): boolean
items.none(.price < 0) // => truecount
The number of items, or of items for which the lambda is true.
count(T[]): numbercount(T[], (T, number) => boolean): number
items.count() // => 3
items.count(.qty > 1) // => 1flatMap
Transforms each item and flattens list results one level.
flatMap(T[], (T, number) => U[] | U): U[]
[1, 2].flatMap([., . * 10]) // => [1, 10, 2, 20]reduce
Folds a list into one value: reduce(list, (acc, item) => ..., initial). Name both parameters; "." is not allowed, since the first one is the accumulator.
reduce(T[], (U, T) => U, U): U
items.reduce((total, it) => total + it.price * it.qty, 0) // => 48sort
Sorts numbers, text, timestamps, or durations; pass "desc" to reverse.
sort(T[], ("asc" | "desc")?): T[]
[3, 1, 2].sort() // => [1, 2, 3]
["b", "a"].sort("desc") // => ["b", "a"]sortBy
Sorts by a key; pass "desc" to reverse. Nulls sort first ("asc") or last ("desc").
sortBy(T[], (T, number) => K, ("asc" | "desc")?): T[]
items.sortBy(.price, "desc").map(.name) // => ["Bag", "Book", "Pen"]groupBy
Groups items into a map of lists by a key. Map keys are text, so a key is its text form: 1 and "1", or true and "true", share a group. A key must be a string, number, or boolean (null is an error). Keys are listed integer-like first, ascending, then in first-seen order.
groupBy(T[], (T, number) => string | number | boolean): { [key: string]: T[] }
items.groupBy(.qty > 1 ? "bulk" : "single").keys() // => ["bulk", "single"]
[1, 2, 3, 4].groupBy(. % 2 == 0 ? "even" : "odd") // => { odd: [1, 3], even: [2, 4] }
[1, "1", 2].groupBy(.) // => { "1": [1, "1"], "2": [2] }
["b", "10", "a", "2"].groupBy(.).keys() // => ["2", "10", "b", "a"]reverse
The items in reverse order.
reverse(T[]): T[]
[1, 2, 3].reverse() // => [3, 2, 1]unique
The items without duplicates (by value), in first-seen order.
unique(T[]): T[]
[1, 2, 1, 3, 2].unique() // => [1, 2, 3]
[{ a: 1 }, { a: 1 }].unique() // => [{ a: 1 }]flat
Flattens nested lists one level.
flat(any[]): any[]
[[1, 2], [3], 4].flat() // => [1, 2, 3, 4]first
The first item, or null.
first(T[]): T | null
items.first().name // => "Pen"
[].first() // => nulllast
The last item, or null.
last(T[]): T | null
[1, 2, 3].last() // => 3join
Joins items into text with a separator (default ",").
join((string | number | boolean | null | timestamp | duration)[], string?): string
items.map(.name).join(", ") // => "Pen, Book, Bag"
[1, 2, 3].join() // => "1,2,3"slice
A section of text or a list; negative positions count from the end.
slice(T[], number, number?): T[]
[1, 2, 3, 4].slice(1, 3) // => [2, 3]
[1, 2, 3, 4].slice(-2) // => [3, 4]at
The item or character at a position; negative positions count from the end.
at(T[], number): T | null
[1, 2, 3].at(-1) // => 3
[1, 2, 3].at(5) // => nullincludes
Whether text contains a substring, or a list contains a value.
includes(T[], any): boolean
[1, 2, 3].includes(2) // => true
[{ id: 1 }].includes({ id: 1 }) // => trueindexOf
Position of the first match, or -1.
indexOf(T[], any): number
["a", "b", "c"].indexOf("b") // => 1lastIndexOf
Position of the last match, or -1.
lastIndexOf(T[], any): number
[1, 2, 1].lastIndexOf(1) // => 2isEmpty
Whether a list, text, or map has no items; null is empty.
isEmpty(any[] | string | { [key: string]: any } | null): boolean
[].isEmpty() // => true
isEmpty(null) // => truemin
The smallest value (nulls are skipped); null for an empty list.
min(T[]): T | null
items.map(.price).min() // => 2
[].min() // => nullmax
The largest value (nulls are skipped); null for an empty list.
max(T[]): T | null
items.map(.price).max() // => 30sum
The sum of the numbers in a list (nulls are skipped), added left to right as a + b + c is.
sum((number | null)[]): numbersum((duration | null)[]): duration
items.map(.price * .qty).sum() // => 48
[1, null, 2].sum() // => 3avg
The mean of the numbers or durations in a list (nulls are skipped, summed as sum does); null for an empty list.
avg((number | null)[]): number | nullavg((duration | null)[]): duration | null
items.map(.price).avg() // => 14.666666666666666
[].avg() // => null