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.

Generally speaking, you don't have to worry about the distinction between indexed and keyed sequence types. The best way to think about sequences is that Seq.Indexed is like a lazy List sequence type while Seq.Keyed is like a lazy Map sequence type.

Creating sequences directly like this isn't common, because they don't have the persistent change methods of other collections such as lists or maps. Sequences are for transforming data for immediate use, not mutating data for later use.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.97.219