Iterates through this sequence.
True if all elements pass the predicate
from([2, 1, 5]).all(x => x >= 1)
// => true
from([2, 1, 5]).all(x => x >= 2)
// => false
True if any elements pass the predicate
from([2, 1, 5]).any(x => x >= 4)
// => true
from([2, 1, 5]).any(x => x <= 0)
// => false
True if the sequence is not empty
from([2, 1, 5]).any()
// => true
from([]).any()
// => false
Projects each element to a number and averages the sequence. If empty, throws.
from(['2', '1', '6']).average(x => parseFloat(x))
// => 3
True if the element is in the sequence. Checked with ===.
from([2, 1, 5]).contains(1)
// => true
from([{ a: '1' }]).contains({ a: '1' })
// => false (strict equality. use any with a custom search instead.)
Counts the number of elements in the sequence
from([2, 1, 5]).count()
// => 3
from([]).count()
// => 0
Get the first element in the sequence. Will throw if empty! Use firstOrDefault if no throw is wanted.
from([2, 1, 5]).first()
// => 2
from([]).first()
// => throws
Get the first element in the sequence. If empty, returns undefined.
from([2, 1, 5]).firstOrDefault()
// => 2
from([]).firstOrDefault()
// => undefined
Project each element to get a key, and group all items by that key.
from([10, 15, 20]).groupBy(x => Math.trunc(x / 10))
.map(g => ({ key: g.key, values: Array.from(g) }))
// => [{ key: 1, values: [10, 15] },
// { key: 2, values: [20] }]
Project each element to get a key, and group all items, each projected onto another type.
from([10, 15, 20]).groupBy(x => Math.trunc(x / 10), x => x.toString())
.map(g => ({ key: g.key, values: Array.from(g) }))
// => [{ key: 1, values: ['10', '15'] },
// { key: 2, values: ['20'] }]
Correlates the elements of two sequences based on matching keys, and groups everything in the other table.
const appleTypes = [
{ name: 'green apple', id: 5 },
{ name: 'red apple', id: 2 },
{ name: 'yellow apple', id: 10 }
];
const apples = [
{ name: 'golden delicious', type: 10 },
{ name: 'granny smith', type: 5 },
{ name: 'pink lady', type: 2 },
{ name: 'fuji', type: 2 },
{ name: 'unknown', type: 999 }
];
from(appleTypes).groupJoin(
apples,
type => type.id,
apple => apple.type,
(type, apples) => `${type.name}: ${apples.map(a => a.name).joinString(', ')}`
);
// => [ 'green apple: granny smith',
// 'red apple: pink lady, fuji',
// 'yellow apple: golden delicious' ]
Correlates the elements of two sequences based on matching keys. An inner join.
const appleTypes = [
{ name: 'green apple', id: 5 },
{ name: 'red apple', id: 2 },
{ name: 'yellow apple', id: 10 }
];
const apples = [
{ name: 'golden delicious', type: 10 },
{ name: 'granny smith', type: 5 },
{ name: 'pink lady', type: 2 },
{ name: 'fuji', type: 2 },
{ name: 'unknown', type: 999 }
];
from(apples).join(
appleTypes,
apple => apple.type,
type => type.id,
(apple, type) => `${apple.name}: ${type.name}`
)
// => [ 'golden delicious: yellow apple',
// 'granny smith: green apple',
// 'pink lady: red apple',
// 'fuji: red apple' ]
Get the last element in the sequence. Will throw if empty! Use lastOrDefault if no throw is wanted.
from([2, 1, 5]).last()
// => 5
from([]).last()
// => throws
Get the last element in the sequence. If empty, returns undefined.
from([2, 1, 5]).lastOrDefault()
// => 5
from([]).lastOrDefault()
// => undefined
Projects each element to a number and finds the max of the sequence. If empty, throws.
from(['2', '1', '5']).max(x => parseFloat(x))
// => 5
Projects each element to a number and finds the min of the sequence. If empty, throws.
from(['2', '1', '5']).min(x => parseFloat(x))
// => 1
True if no elements pass the predicate
from([2, 1, 5]).none(x => x === 0)
// => true
from([2, 1, 5]).none(x => x === 1)
// => false
Sort the array in ascending order of the selector
from([4, 1, 10]).orderBy(x => x)
// => [1, 4, 10]
Sort the array in ascending order of the selector, with a custom comparer
// Sort alphabetically, ignoring case.
from(['A xylophone', 'a frog', 'a zoo']).orderBy(x => x, new Intl.Collator('en', { sensitivity: 'base' }).compare)
// => ['a frog', 'A xylophone', 'a zoo']
Sort the array in descending order of the selector
from([4, 1, 10]).orderByDescending(x => x)
// => [10, 4, 1]
Sort the array in descending order of the selector, with a custom comparer
// Sort reverse alphabetically, ignoring case.
from(['A xylophone', 'a frog', 'a zoo']).orderByDescending(x => x, new Intl.Collator('en', { sensitivity: 'base' }).compare)
// => ['a zoo', 'A xylophone', 'a frog']
Get the only element in the sequence. Will throw if empty or more than one element! Use singleOrDefault if no throw is wanted.
from([2]).single()
// => 2
from([2, 1, 5]).single()
// => throws
from([]).single()
// => throws
Get the only element in the sequence. Returns undefined if empty or more than one element.
from([2]).singleOrDefault()
// => 2
from([2, 1, 5]).singleOrDefault()
// => undefined
Projects each element to a number and sums the sequence. If empty, returns 0.
from(['2', '1', '5']).sum(x => parseFloat(x))
// => 8
Converts this sequence to an array.
Note: If you are using this Sequence in a for..of loop, you do not need this --
you can use the sequence directly.
from([2, 1, 5]).toArray()
// => [2, 1, 5]
Convert this sequence into a Set
from([2, 1, 1, 5]).toSet()
// => new Set([2, 1, 5])
A sequence of values.
To create me, use from: