Creates a new Collection.Indexed.
Note: Collection.Indexed
is a conversion function and not a class, and
does not use the new
keyword during construction.
Always returns Seq.Indexed, discarding associated keys and supplying incrementing indices.
Note: Seq.Indexed
is a conversion function and not a class, and does
not use the new
keyword during construction.
Indexed Collections have incrementing numeric keys. They exhibit
slightly different behavior than Collection.Keyed
for some methods in order
to better mirror the behavior of JavaScript's Array
, and add methods
which do not make sense on non-indexed Collections such as indexOf
.
Unlike JavaScript arrays, Collection.Indexed
s are always dense. "Unset"
indices and undefined
indices are indistinguishable, and all indices from
0 to size
are visited when iterated.
All Collection.Indexed methods return re-indexed Collections. In other words,
indices always start at 0 and increment until size. If you wish to
preserve indices, using them as keys, convert to a Collection.Keyed by
calling toKeyedSeq
.
Indexed Collections have incrementing numeric keys. They exhibit
slightly different behavior than Collection.Keyed
for some methods in order
to better mirror the behavior of JavaScript's Array
, and add methods
which do not make sense on non-indexed Collections such as indexOf
.
Unlike JavaScript arrays, Collection.Indexed
s are always dense. "Unset"
indices and undefined
indices are indistinguishable, and all indices from
0 to size
are visited when iterated.
All Collection.Indexed methods return re-indexed Collections. In other words,
indices always start at 0 and increment until size. If you wish to
preserve indices, using them as keys, convert to a Collection.Keyed by
calling toKeyedSeq
.
Some Seqs can describe their size lazily. When this is the case, size will be an integer. Otherwise it will be undefined.
For example, Seqs returned from map()
or reverse()
preserve the size of the original Seq
while filter()
does not.
Note: Range
, Repeat
and Seq
s made from Array
s and Object
s will
always have a size.
Creates a Collection.
The type of Collection created is based on the input.
Collection
, that same Collection
.Collection.Indexed
.Collection.Indexed
.Collection.Keyed
.This methods forces the conversion of Objects and Strings to Collections.
If you want to ensure that a Collection of one item is returned, use
Seq.of
.
Note: An Iterator itself will be treated as an object, becoming a Seq.Keyed
,
which is usually not what you want. You should turn your Iterator Object into
an iterable object by defining a Symbol.iterator (or @@iterator) method which
returns this
.
Note: Collection
is a conversion function and not a class, and does not
use the new
keyword during construction.
Creates a Collection.
The type of Collection created is based on the input.
Collection
, that same Collection
.Collection.Indexed
.Collection.Indexed
.Collection.Keyed
.This methods forces the conversion of Objects and Strings to Collections.
If you want to ensure that a Collection of one item is returned, use
Seq.of
.
Note: An Iterator itself will be treated as an object, becoming a Seq.Keyed
,
which is usually not what you want. You should turn your Iterator Object into
an iterable object by defining a Symbol.iterator (or @@iterator) method which
returns this
.
Note: Collection
is a conversion function and not a class, and does not
use the new
keyword during construction.
Creates a Seq.
Returns a particular kind of Seq
based on the input.
Seq
, that same Seq
.Collection
, a Seq
of the same kind (Keyed, Indexed, or Set).Seq.Indexed
.Seq.Indexed
.Seq.Keyed
.Note: An Iterator itself will be treated as an object, becoming a Seq.Keyed
,
which is usually not what you want. You should turn your Iterator Object into
an iterable object by defining a Symbol.iterator (or @@iterator) method which
returns this
.
Note: Seq
is a conversion function and not a class, and does not use the
new
keyword during construction.
Because Sequences are lazy and designed to be chained together, they do
not cache their results. For example, this map function is called a total
of 6 times, as each join
iterates the Seq of three values.
var squares = Seq([ 1, 2, 3 ]).map(x => x * x)
squares.join() + squares.join()
If you know a Seq
will be used multiple times, it may be more
efficient to first cache it in memory. Here, the map function is called
only 3 times.
var squares = Seq([ 1, 2, 3 ]).map(x => x * x).cacheResult()
squares.join() + squares.join()
Use this method judiciously, as it must fully evaluate a Seq which can be a burden on memory and possibly performance.
Note: after calling cacheResult
, a Seq will always have a size
.
Provides an Seq.Indexed of the values provided.
Returns a new Collection with other collections concatenated to this one.
Returns a new Seq with other collections concatenated to this one.
Returns a new Collection with only the values for which the predicate
function returns true.
Note: filter()
always returns a new instance, even if it results in
not filtering out any values.
Returns a new Seq with only the values for which the predicate
function returns true.
Note: filter()
always returns a new instance, even if it results in
not filtering out any values.
Returns the first index in the Collection where a value satisfies the provided predicate function. Otherwise -1 is returned.
Returns the last index in the Collection where a value satisfies the provided predicate function. Otherwise -1 is returned.
Flat-maps the Collection, returning a Collection of the same type.
Similar to collection.map(...).flatten(true)
.
Flat-maps the Seq, returning a a Seq of the same type.
Similar to seq.map(...).flatten(true)
.
If this is a collection of [key, value] entry tuples, it will return a Seq.Keyed of those entries.
Returns the value associated with the provided index, or notSetValue if the index is beyond the bounds of the Collection.
index
may be a negative number, which indexes back from the end of the
Collection. s.get(-1)
gets the last item in the Collection.
Returns the first index at which a given value can be found in the Collection, or -1 if it is not present.
Returns a Collection of the same type with the provided collections
interleaved into this collection.
The resulting Collection includes the first item from each, then the second from each, etc.
const { List } = require('immutable')
List([ 1, 2, 3 ]).interleave(List([ 'A', 'B', 'C' ]))
// List [ 1, "A", 2, "B", 3, "C"" ]
The shortest Collection stops interleave.
List([ 1, 2, 3 ]).interleave(
List([ 'A', 'B' ]),
List([ 'X', 'Y', 'Z' ])
)
// List [ 1, "A", "X", 2, "B", "Y"" ]
Since interleave()
re-indexes values, it produces a complete copy,
which has O(N)
complexity.
Note: interleave
cannot be used in withMutations
.
Returns a Collection of the same type with separator
between each item
in this Collection.
Returns the last index at which a given value can be found in the Collection, or -1 if it is not present.
Returns a new Collection.Indexed with values passed through a
mapper
function.
const { Collection } = require('immutable')
Collection.Indexed([1,2]).map(x => 10 * x)
// Seq [ 1, 2 ]
Note: map()
always returns a new instance, even if it produced the
same value at every step.
Returns a new Seq.Indexed with values passed through a
mapper
function.
const { Seq } = require('immutable')
Seq.Indexed([ 1, 2 ]).map(x => 10 * x)
// Seq [ 10, 20 ]
Note: map()
always returns a new instance, even if it produced the
same value at every step.
Splice returns a new indexed Collection by replacing a region of this Collection with new values. If values are not provided, it only skips the region to be removed.
index
may be a negative number, which indexes back from the end of the
Collection. s.splice(-2)
splices after the second to last item.
const { List } = require('immutable')
List([ 'a', 'b', 'c', 'd' ]).splice(1, 2, 'q', 'r', 's')
// List [ "a", "q", "r", "s", "d" ]
Since splice()
re-indexes values, it produces a complete copy, which
has O(N)
complexity.
Note: splice
cannot be used in withMutations
.
Shallowly converts this collection to an Array.
Shallowly converts this collection to an Array.
Deeply converts this Indexed collection to equivalent native JavaScript Array.
Deeply converts this Indexed Seq to equivalent native JavaScript Array.
Shallowly converts this Indexed collection to equivalent native JavaScript Array.
Shallowly converts this Indexed Seq to equivalent native JavaScript Array.
Returns Seq.Indexed.
Returns itself
Returns a Collection of the same type "zipped" with the provided collections.
Like zipWith
, but using the default zipper
: creating an Array
.
const a = List([ 1, 2, 3 ]);
const b = List([ 4, 5, 6 ]);
const c = a.zip(b); // List [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
Returns a Seq "zipped" with the provided collections.
Like zipWith
, but using the default zipper
: creating an Array
.
const a = Seq([ 1, 2, 3 ]);
const b = Seq([ 4, 5, 6 ]);
const c = a.zip(b); // Seq [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
Returns a Collection "zipped" with the provided collections.
Unlike zip
, zipAll
continues zipping until the longest collection is
exhausted. Missing values from shorter collections are filled with undefined
.
const a = List([ 1, 2 ]);
const b = List([ 3, 4, 5 ]);
const c = a.zipAll(b); // List [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
Returns a Seq "zipped" with the provided collections.
Unlike zip
, zipAll
continues zipping until the longest collection is
exhausted. Missing values from shorter collections are filled with undefined
.
const a = Seq([ 1, 2 ]);
const b = Seq([ 3, 4, 5 ]);
const c = a.zipAll(b); // Seq [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
Returns a Collection of the same type "zipped" with the provided
collections by using a custom zipper
function.
const a = List([ 1, 2, 3 ]);
const b = List([ 4, 5, 6 ]);
const c = a.zipWith((a, b) => a + b, b);
// List [ 5, 7, 9 ]
Returns a Seq "zipped" with the provided collections by using a
custom zipper
function.
const a = Seq([ 1, 2, 3 ]);
const b = Seq([ 4, 5, 6 ]);
const c = a.zipWith((a, b) => a + b, b);
// Seq [ 5, 7, 9 ]
Generated using TypeDoc
Indexed Collections have incrementing numeric keys. They exhibit slightly different behavior than
Collection.Keyed
for some methods in order to better mirror the behavior of JavaScript'sArray
, and add methods which do not make sense on non-indexed Collections such asindexOf
.Unlike JavaScript arrays,
Collection.Indexed
s are always dense. "Unset" indices andundefined
indices are indistinguishable, and all indices from 0 tosize
are visited when iterated.All Collection.Indexed methods return re-indexed Collections. In other words, indices always start at 0 and increment until size. If you wish to preserve indices, using them as keys, convert to a Collection.Keyed by calling
toKeyedSeq
.