Sequence creation and iteration
There are two ways to create sequences: you can directly create sequence instances, passing them collection data, or you can transform existing collections, such as lists and maps, into sequences.
Basic sequence creation
You can create new sequences by passing the Seq()
constructor JavaScript arrays or objects, as follows:
import { Seq } from 'immutable'; const myIndexedSeq = Seq([1, 2, 3]); const myKeyedSeq = Seq({ one: 1, two: 2, three: 3 }); console.log('myIndexedSeq', myIndexedSeq.toJS()); // -> myIndexedSeq [ 1, 2, 3 ] console.log('myKeyedSeq', myKeyedSeq.toJS()); // -> myKeyedSeq { one: 1, two: 2, three: 3 }
There are actually two types of sequence collections: Seq.Indexed
and Seq.Keyed
. The Seq()
constructor, which is really just a function due to the absence of the new
keyword, will return the correct type of collection depending on what's passed to it.
Note
Generally speaking, you don't have to worry about the distinction between indexed and keyed...