List

List是类似于JS中数组的密集型有序集合。

  1. class List<T> extends Collection.Indexed<T>

List是不可变的(Immutable),修改和读取数据的复杂度为O(log32 N),入栈出栈(push, pop)复杂度为O(1)。

List实现了队列功能,能高效的在队首(unshift, shift)或者队尾(push, pop)进行元素的添加和删除。

与JS的数组不同,在List中一个未设置的索引值和设置为undefined的索引值是相同的。List#forEach会从0到size便利所有元素,无论他是否有明确定义。

构造函数
List()

新建一个包含传入元素的不可变List。

  1. List(): List<any>
  2. List<T>(): List<T>
  3. List<T>(collection: Iterable<T>): List<T>

  1. const { List, Set } = require('immutable')
  2. const emptyList = List()
  3. // List []
  4. const plainArray = [ 1, 2, 3, 4 ]
  5. const listFromPlainArray = List(plainArray)
  6. // List [ 1, 2, 3, 4 ]
  7. const plainSet = Set([ 1, 2, 3, 4 ])
  8. const listFromPlainSet = List(plainSet)
  9. // List [ 1, 2, 3, 4 ]
  10. const arrayIterator = plainArray[Symbol.iterator]()
  11. const listFromCollectionArray = List(arrayIterator)
  12. // List [ 1, 2, 3, 4 ]
  13. listFromPlainArray.equals(listFromCollectionArray) // true
  14. listFromPlainSet.equals(listFromCollectionArray) // true
  15. listFromPlainSet.equals(listFromPlainArray) // true
静态方法
List.isList()

判断传入参数是否为List。

  1. List.isList(maybeList: any): boolean

  1. List.isList([]); // false
  2. List.isList(List()); // true
List.of()

使用传入值新建一个List。

  1. List.of<T>(...values: Array<T>): List<T>

  1. List.of(1, 2, 3, 4)
  2. // List [ 1, 2, 3, 4 ]

注意:所有值不会被改变。

  1. List.of({x:1}, 2, [3], 4)
  2. // List [ { x: 1 }, 2, [ 3 ], 4 ]
成员变量
size
  1. size
持久化修改
set()

返回一个在index位置处值为value的新List。如果index位置已经定义了值,它将会被替换。

  1. set(index: number, value: T): List<T>

index为负值时,将从List尾部开始索引。v.set(-1, "value")设置了List最后一个索引位置的值。

index比原List的size大时,返回的新List的size将会足够大以包含index

  1. const originalList = List([ 0 ]);
  2. // List [ 0 ]
  3. originalList.set(1, 1);
  4. // List [ 0, 1 ]
  5. originalList.set(0, 'overwritten');
  6. // List [ "overwritten" ]
  7. originalList.set(2, 2);
  8. // List [ 0, undefined, 2 ]
  9. List().set(50000, 'value').size;
  10. // 50001

注意:set可以在withMutations中使用。

delete()

返回一个不包含原index值总长度减一的新List。并且大于原index的索引都会减一。

  1. delete(index: number): List<T>
别名

remove()

此方法与list.splice(index, 1)是同义的。

index可以为负值,表示从末尾开始计算索引。v.delete(-1)将会删除List最后一个元素。

注意:delete在IE8上使用是不安全的。

  1. List([ 0, 1, 2, 3, 4 ]).delete(0);
  2. // List [ 1, 2, 3, 4 ]

注意:delete不可withMutations中使用。

insert()

返回一个index处值为value总长度加一的新List。并且大于原index的索引都会加一。

  1. insert(index: number, value: T): List<T>

此方法与list.splice(index, 0, value)同义。

注意:insert不可withMutations中使用。

clear()

返回一个新的长度为0的空List。

  1. clear(): List<T>

  1. List([ 1, 2, 3, 4 ]).clear()
  2. // List []

注意:clear可以在withMutations中使用。

push()

返回一个新的List为旧List末尾添加一个元素值为所传入value

  1. push(...values: Array<T>): List<T>

  1. List([ 1, 2, 3, 4 ]).push(5)
  2. // List [ 1, 2, 3, 4, 5 ]

注意:push可以在withMutations中使用。

pop()

返回一个新的List为旧List移除最后一个元素。

  1. pop(): List<T>

注意:这与Array#pop不同,它返回的是新List而不是被移除元素。使用last()方法来获取List最后一个元素。

  1. List([ 1, 2, 3, 4 ]).pop()
  2. // List[ 1, 2, 3 ]

注意:pop可以在withMutations中使用。

unshift()

返回一个新的List为旧List头部插入所提供的values

  1. unshift(...values: Array<T>): List<T>

  1. List([ 2, 3, 4]).unshift(1);
  2. // List [ 1, 2, 3, 4 ]

注意:unshift可以在withMutations中使用。

shift()

返回一个新的List为旧List移除第一个元素,并且其他元素索引减一。

  1. shift(): List<T>

注意:这与Array#shift不同,它返回的是新List而不是被移除元素。使用first()方法来获取List最后一个元素。

  1. List([ 0, 1, 2, 3, 4 ]).shift();
  2. // List [ 1, 2, 3, 4 ]

注意:shift可以在withMutations中使用。

update()

将会返回一个新List,如果指定index位置在原List中存在,那么由所提供函数updater更新此位置值,否则该位置值设为所提供的notSetValue值。 如果只传入了一个参数updater,那么updater接受的参数为List本身。

  1. update(index: number, notSetValue: T, updater: (value: T) => T): this
  2. update(index: number, updater: (value: T) => T): this
  3. update<R>(updater: (value: this) => R): R

重载

Collection#update

Map#update

index可以为负值,表示从尾部开始索引。v.update(-1)表示更新最后一个元素。

  1. const list = List([ 'a', 'b', 'c' ])
  2. const result = list.update(2, val => val.toUpperCase())
  3. // List [ "a", "b", "C" ]

此方法可以很方便地链式调用一系列普通的方法。RxJS称这为”let”,lodash叫做”thru”。

例,在调用map和filter之后计算List的和:

  1. function sum(collection) {
  2. return collection.reduce((sum, x) => sum + x, 0)
  3. }
  4. List([ 1, 2, 3 ])
  5. .map(x => x + 1)
  6. .filter(x => x % 2 === 0)
  7. .update(sum)
  8. // 6

注意:update(index)可以在withMutations中使用。

merge()

注意:merge可以在withMutations中使用。

  1. merge(...collections: Array<Collection.Indexed<T> | Array<T>>): this

Map#merge

mergeWith()

注意:mergeWith可以在withMutations中使用。

  1. mergeWith(
  2. merger: (oldVal: T, newVal: T, key: number) => T,
  3. ...collections: Array<Collection.Indexed<T> | Array<T>>
  4. ): this

Map#mergeWith

mergeDeep()

注意:mergeDeep可以在withMutations中使用。

  1. mergeDeep(...collections: Array<Collection.Indexed<T> | Array<T>>): this

Map#mergeDeep

mergeDeepWith()

注意:mergeDeepWith可以在withMutations中使用。

  1. mergeDeepWith(
  2. merger: (oldVal: T, newVal: T, key: number) => T,
  3. ...collections: Array<Collection.Indexed<T> | Array<T>>
  4. ): this

Map#mergeDeepWith

setSize()

返回一个新的List,长度为指定size。如果原长度大于size,那么新的list将不包含超size的值。如果原长度小于size,那么超过部分的值为undefind。

  1. setSize(size: number): List<T>

在新构建一个LIst时,当已知最终长度,setSize可能会和withMutations共同使用以提高性能。

深度持久化
setIn()

返回一个新的在ksyPath指定位置了值value的List。如果在keyPath位置没有值存在,这个位置将会被构建一个新的不可变Map。

  1. setIn(keyPath: Iterable<any>, value: any): this

数值索引将被用于描述在List中的路径位置。

  1. const { List } = require('immutable');
  2. const list = List([ 0, 1, 2, List([ 3, 4 ])])
  3. list.setIn([3, 0], 999);
  4. // List [ 0, 1, 2, List [ 999, 4 ] ]

注意:setIn可以在withMutations中使用。

deleteIn()

返回一个删除了由keyPath指定位置值的新List。如果指定位置无值,那么不会发生改变。

  1. deleteIn(keyPath: Iterable<any>): this

别名

removeIn()

  1. const { List } = require('immutable');
  2. const list = List([ 0, 1, 2, List([ 3, 4 ])])
  3. list.deleteIn([3, 0]);
  4. // List [ 0, 1, 2, List [ 4 ] ]

注意:removeIn不可withMutations中使用。

updateIn()

注意:updateIn可以在withMutations中使用。

  1. updateIn(
  2. keyPath: Iterable<any>,
  3. notSetValue: any,
  4. updater: (value: any) => any
  5. ): this
  6. updateIn(keyPath: Iterable<any>, updater: (value: any) => any): this

Map#updateIn

mergIn()

注意:mergIn不可withMutations中使用。

  1. mergeIn(keyPath: Iterable<any>, ...collections: Array<any>): this

Map#mergIn

mergeDeepIn()

注意:mergeDeepIn可以在withMutations中使用。

  1. mergeDeepIn(keyPath: Iterable<any>, ...collections: Array<any>): this

Map#mergeDeepIn

暂时改变
withMutaitions()

注意:只有部分方法可以被可变的集合调用或者在withMutations中调用!查看文档中各个方法看他是否允许在withMuataions中调用。

  1. withMutations(mutator: (mutable: this) => any): this

Map#withMutations

asMutable()

withMutations()的替代API。

  1. asMutable(): this

Map#asMutable

注意:只有部分方法可以被可变的集合调用或者在withMutations中调用!查看文档中各个方法看他是否允许在withMuataions中调用。

  1. withMutations(mutator: (mutable: this) => any): this
asImmutable()
  1. asImmutable(): this

Map#asImmutable

序列算法
cancat()

将其他的值或者集合与这个List串联起来返回为一个新List。

  1. concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): List<T | C>

覆盖

Collection#concat

map()

返回一个由传入的mapper函数处理过值的新List。

  1. map<M>(mapper: (value: T, key: number, iter: this) => M, context?: any): List<M>

覆盖

Collection#map

  1. List([ 1, 2 ]).map(x => 10 * x)
  2. // List [ 10, 20 ]

注意:map()总是返回一个新的实例,即使它产出的每一个值都与原始值相同。

flatMap()

扁平化这个List为一个新List。

  1. flatMap<M>(
  2. mapper: (value: T, key: number, iter: this) => Iterable<M>,
  3. context?: any
  4. ): List<M>

覆盖

Collection#flatMap

list.map(...).flatten(true)相似。

filter()

返回一个只有由传入方法predicate返回为true的值组成的新LIst。

  1. filter<F>(
  2. predicate: (value: T, index: number, iter: this) => boolean,
  3. context?: any
  4. ): List<F>
  5. filter(
  6. predicate: (value: T, index: number, iter: this) => any,
  7. context?: any
  8. ): this

覆盖

Collection#filter

注意:filter()总是返回一个新的实例,即使它的结果没有过滤掉任何一个值。

zip()

将List与所提供集合拉链咬合(zipped)。

  1. zip(...collections: Array<Collection<any, any>>): List<any>

覆盖

Collection.Index#zip

zipWIth类似,但这个使用默认的zipper建立数组

  1. const a = List([ 1, 2, 3 ]);
  2. const b = List([ 4, 5, 6 ]);
  3. const c = a.zip(b); // List [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
zipWith()

将List与所提供集合使用自定义zipper方法进行拉链咬合(zipped)。

  1. zipWith<U, Z>(
  2. zipper: (value: T, otherValue: U) => Z,
  3. otherCollection: Collection<any, U>
  4. ): List<Z>
  5. zipWith<U, V, Z>(
  6. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  7. otherCollection: Collection<any, U>,
  8. thirdCollection: Collection<any, V>
  9. ): List<Z>
  10. zipWith<Z>(
  11. zipper: (...any: Array<any>) => Z,
  12. ...collections: Array<Collection<any, any>>
  13. ): List<Z>

覆盖

Collection.Indexed#zipWith

  1. const a = List([ 1, 2, 3 ]);
  2. const b = List([ 4, 5, 6 ]);
  3. const c = a.zipWith((a, b) => a + b, b);
  4. // List [ 5, 7, 9 ]
[Symbol.iterator]
  1. [Symbol.iterator](): IterableIterator<T>

继承自

Collection.Indexed#[Symbol.iterator]

filterNot()

返回一个由所提供的predicate方法返回false过滤的新的相同类型的集合。

  1. filterNot(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any
  4. ): this

继承自

Collection#filterNot

  1. const { Map } = require('immutable')
  2. Map({ a: 1, b: 2, c: 3, d: 4}).filterNot(x => x % 2 === 0)
  3. // Map { "a": 1, "c": 3 }

注意:filterNot总是返回一个新的实例,即使它没有过滤掉任何一个值。

reverse()

返回为一个逆序的新的List。

  1. reverse(): this

继承自

Collection#reverse

sort()

返回一个使用传入的comparator重新排序的新List。

  1. sort(comparator?: (valueA: T, valueB: T) => number): this

继承自

Collection#sort

如果没有提供comparator方法,那么默认的比较将使用<>

comparator(valueA, valueB):

  • 返回值为0这个元素将不会被交换。
  • 返回值为-1(或者任意负数)valueA将会移到valueB之前。
  • 返回值为1(或者任意正数)valueA将会移到valueB之后。
  • 为空,这将会返回相同的值和顺序。

当被排序的集合没有定义顺序,那么将会返回同等的有序集合。比如map.sort()将返回OrderedMap。

  1. const { Map } = require('immutable')
  2. Map({ "c": 3, "a": 1, "b": 2 }).sort((a, b) => {
  3. if (a < b) { return -1; }
  4. if (a > b) { return 1; }
  5. if (a === b) { return 0; }
  6. });
  7. // OrderedMap { "a": 1, "b": 2, "c": 3 }

注意:sort()总是返回一个新的实例,即使它没有改变排序。

sortBy()

sort类似,但能接受一个comparatorValueMapper方法,它允许通过更复杂的方式进行排序:

  1. sortBy<C>(
  2. comparatorValueMapper: (value: T, key: number, iter: this) => C,
  3. comparator?: (valueA: C, valueB: C) => number
  4. ): this

继承自

Collection#sortBy

  1. hitters.sortBy(hitter => hitter.avgHits)

注意:sortBy()总是返回一个新的实例,即使它没有改变排序。

groupBy()

返回一个Collection.KeyedsCollection.keyed,由传入的grouper方法分组。

  1. groupBy<G>(
  2. grouper: (value: T, key: number, iter: this) => G,
  3. context?: any
  4. ): Seq.Keyed<G, Collection<number, T>>

继承自

Collection#groupBy

  1. const { List, Map } = require('immutable')
  2. const listOfMaps = List([
  3. Map({ v: 0 }),
  4. Map({ v: 1 }),
  5. Map({ v: 1 }),
  6. Map({ v: 0 }),
  7. Map({ v: 2 })
  8. ])
  9. const groupsOfMaps = listOfMaps.groupBy(x => x.get('v'))
  10. // Map {
  11. // 0: List [ Map{ "v": 0 }, Map { "v": 0 } ],
  12. // 1: List [ Map{ "v": 1 }, Map { "v": 1 } ],
  13. // 2: List [ Map{ "v": 2 } ],
  14. // }
转换为JavaScript类型
toJS()

深层地将这个有序的集合转换转换为原生JS数组。

  1. toJS(): Array<any>

继承自

Collection.Index#toJS

toJSON()

浅转换这个有序的集合为原生JS数组。

  1. toJSON(): Array<any>

继承自

Collection.Index#toJSON

toArray()

浅转换这个有序的集合为原生JS数组并且丢弃key。

  1. toArray(): Array<any>

继承自

Collection#toArray

toObject()

浅转换这个有序的集合为原生JS对象。

  1. toObject(): {[key: string]: V}

继承自

Collection#toObject

读值
get()

返回提供的索引位置关联的值,或者当提供的索引越界时返回所提供的notSetValue。

  1. get<NSV>(index: number, notSetValue: NSV): T | NSV
  2. get(index: number): T | undefined

继承自

Collection.Indexed#get

index可以为负值,表示从集合尾部开始索引。s.get(-1)取得集合最后一个元素。

has()

使用Immutable.is判断key值是否在Collection中。

  1. has(key: number): boolean

继承自

Collection#has

includes()

使用Immutable.is判断value值是否在Collection中。

  1. includes(value: T): boolean

继承自

Collection#includes

first()

取得集合第一个值。

  1. first(): T | undefined

继承自

Collection#first

last()

取得集合第一个值。

  1. last(): T | undefined

继承自

Collection#last

转换为Seq
toSeq()

返回Seq.Indexed。

  1. toSeq(): Seq.Indexed<T>

继承自

Collection.Indexed#toSeq

fromEntrySeq()

如果这个集合是由[key, value]这种原组构成的,那么这将返回这些原组的Seq.Keyed。

  1. fromEntrySeq(): Seq.Keyed<any, any>

继承自

Collection.Index#fromEntrySeq

toKeyedSeq()

从这个集合返回一个Seq.Keyed,其中索引将视作key。

  1. toKeyedSeq(): Seq.Keyed<number, T>

继承自

Collection#toKeyedSeq

如果你想对Collection.Indexed操作返回一组[index, value]对,这将十分有用。

返回的Seq将与Colleciont有相同的索引顺序。

  1. const { Seq } = require('immutable')
  2. const indexedSeq = Seq([ 'A', 'B', 'C' ])
  3. // Seq [ "A", "B", "C" ]
  4. indexedSeq.filter(v => v === 'B')
  5. // Seq [ "B" ]
  6. const keyedSeq = indexedSeq.toKeyedSeq()
  7. // Seq { 0: "A", 1: "B", 2: "C" }
  8. keyedSeq.filter(v => v === 'B')
  9. // Seq { 1: "B" }
toIndexedSeq()

将这个集合的值丢弃键(key)返回为Seq.Indexed。

  1. toIndexedSeq(): Seq.Indexed<T>

继承自

Collection#toIndexedSeq

toSetSeq()

将这个集合的值丢弃键(key)返回为Seq.Set。

  1. toSetSeq(): Seq.Set<T>

继承自

Collection#toSetSeq

组合
interpose()

返回一个在原集合每两个元素之间插入提供的separator的同类型集合。

  1. interpose(separator: T): this

继承自

COllection.Indexed#interpose

interleave()

返回一个原集合与所提供collections交叉的痛类型集合。

  1. interleave(...collections: Array<Collection<any, T>>): this

继承自

Collection.Indexed#interleave

返回的集合依次包含第一个集合元素与第二个集合元素。

  1. const { List } = require('immutable')
  2. List([ 1, 2, 3 ]).interleave(List([ 'A', 'B', 'C' ]))
  3. // List [ 1, "A", 2, "B", 3, "C"" ]

由最短的集合结束交叉。

  1. List([ 1, 2, 3 ]).interleave(
  2. List([ 'A', 'B' ]),
  3. List([ 'X', 'Y', 'Z' ])
  4. )
  5. // List [ 1, "A", "X", 2, "B", "Y"" ]
splice()

返回一个由指定值替换了原集合某个范围的值的新的有序集合。如果没提供替换的值,那么会跳过要删除的范围。

  1. splice(index: number, removeNum: number, ...values: Array<T>): this

继承自

Collection.Indexed#splice

index可以为负值,表示从集合结尾开始索引。s.splice(-2)表示倒数第二个元素开始拼接。

  1. const { List } = require('immutable')
  2. List([ 'a', 'b', 'c', 'd' ]).splice(1, 2, 'q', 'r', 's')
  3. // List [ "a", "q", "r", "s", "d" ]
flatten()

压平嵌套的集合。

  1. flatten(depth?: number): Collection<any, any>
  2. flatten(shallow?: boolean): Collection<any, any>

继承自

Collection#flatten

默认会深度地经常压平集合操作,返回一个同类型的集合。可以指定depth为压平深度或者是否深度压平(为true表示仅进行一层的浅层压平)。如果深度为0(或者shllow:false)将会深层压平。

压平仅会操作其他集合,数组和对象不会进行此操作。

注意:flatten(true)操作是在集合上进行,同时返回一个集合。

查找
indexOf()

返回集合中第一个与所提供的搜索值匹配的索引,无匹配值则返回-1。

  1. indexOf(searchValue: T): number

继承自

Collection.Indexed#indexOf

lastIndexOf()

返回集合中最后一个与所提供的搜索值匹配的索引,无匹配值则返回-1。

  1. lastIndexOf(searchValue: T): number

继承自

Collection.Indexed#lastIndexOf

findIndex()

返回集合中第一个符合与所提供的断言的索引,均不符合则返回-1。

  1. findIndex(
  2. predicate: (value: T, index: number, iter: this) => boolean,
  3. context?: any
  4. ): number

继承自

Collection.Indexed#findIndex

findLastIndex()

返回集合中最后一个符合与所提供的断言的索引,均不符合则返回-1。

  1. findLastIndex(
  2. predicate: (value: T, index: number, iter: this) => boolean,
  3. context?: any
  4. ): number

继承自

Collection.Indexed#findLastIndex

find()

返回集合中第一个符合与所提供的断言的值。

  1. find(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any,
  4. notSetValue?: T
  5. ): T | undefined

继承自

Collection#find

findLast()

返回集合中最后一个符合与所提供的断言的值。

  1. findLast(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any,
  4. notSetValue?: T
  5. ): T | undefined

继承自

Collection#findLast

注意:predicate将会逆序地在每个值上调用。

findEntry()

返回第一个符合所提供断言的值的[key, value]。

  1. findEntry(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any,
  4. notSetValue?: T
  5. ): [number, T] | undefined

继承自

Collection#findEntry

findLastEntry()

返回最后一个符合所提供断言的值的[key, value]。

  1. findLastEntry(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any,
  4. notSetValue?: T
  5. ): [number, T] | undefined

继承自

Collection#findLastEntry

注意:predicate将会逆序地在每个值上调用。

findKey()

返回第一个predicate返回为true的键。

  1. findKey(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any
  4. ): number | undefined

继承自

Collection#findKey

findLastKey()

返回最后一个predicate返回为true的键。

  1. findLastKey(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any
  4. ): number | undefined

继承自

Collection#findLastKey

注意:predicate将会逆序地在每个值上调用。

keyOf()

返回与提供的搜索值关联的键,或者undefined。

  1. keyOf(searchValue: T): number | undefined

继承自

Collection#keyOf

lastKeyOf()

返回最后一个与提供的搜索值关联的键或者undefined。

  1. lastKeyOf(searchValue: T): number | undefined

继承自

Collection#lastKeyOf

max()

返回集合中最大的值。如果有多个值比较为相等,那么将返回第一个。

  1. max(comparator?: (valueA: T, valueB: T) => number): T | undefined

继承自

Collection#max

comparator的使用方法与Collection#sort是一样的,如果未提供那么默认的比较为>

当两个值比较为相等时,第一个遇见的值将会被返回。另一方面,如果comparator是可交换的,那么max将会独立于输入的顺序。默认的比较器>只有在类型不一致时才可交换。

如果comparator返回0或者值为NaN、undefined或者null,这个值将会被返回。

maxBy()

和max类似,但还能接受一个comparatorValueMapper来实现更复杂的比较。

  1. maxBy<C>(
  2. comparatorValueMapper: (value: T, key: number, iter: this) => C,
  3. comparator?: (valueA: C, valueB: C) => number
  4. ): T | undefined

继承自

Collection#maxBy

  1. hitters.maxBy(hitter => hitter.avgHits)
min()

返回集合中最小的值,如果有多个值比较为相等,将会返回第一个。

  1. min(comparator?: (valueA: T, valueB: T) => number): T | undefined

继承自

Collection#min

当两个值比较为相等时,第一个遇见的值将会被返回。另一方面,如果comparator是可交换的,那么min将会独立于输入的顺序。默认的比较器<只有在类型不一致时才可交换。

如果comparator返回0或者值为NaN、undefined或者null,这个值将会被返回。

minBy()

和min类似,但还能接受一个comparatorValueMapper来实现更复杂的比较。

  1. minBy<C>(
  2. comparatorValueMapper: (value: T, key: number, iter: this) => C,
  3. comparator?: (valueA: C, valueB: C) => number
  4. ): T | undefined

继承自

Collection#minBy

  1. hitters.minBy(hitter => hitter.avgHits)
等值比较
equals()

如果当前集合和另一个集合比较为相等,那么返回true,是否相等由Immutable.is()定义。

  1. equals(other: any): boolean

继承自

Collection#equals

注意:此方法与Immutable.is(this, other)等效,提供此方法是为了方便能够链式地使用。

hashCode()

计算并返回这个集合的哈希值。

  1. hashCode(): number

继承自

Collection#hashCode

集合的hashCode用于确定两个集合的相等性,在添加到Set或者被作为Map的键值时用于检测两个实例是否相等而会被使用到。

  1. const a = List([ 1, 2, 3 ]);
  2. const b = List([ 1, 2, 3 ]);
  3. assert(a !== b); // different instances
  4. const set = Set([ a ]);
  5. assert(set.has(b) === true);

当两个值的hashCode相等时,并不能完全保证他们是相等的,但当他们的hashCode不同时,他们一定是不等的。

读取深层数据
getIn()

返回根据提供的路径或者索引搜索到的嵌套的值。

  1. getIn(searchKeyPath: Iterable<any>, notSetValue?: any): any

继承自

Collection#getIn

hasIn()

根据提供的路径或者索引检测该处是否设置了值。

  1. hasIn(searchKeyPath: Iterable<any>): boolean

继承自

Collection#hasIn

转换为集合
toMap()

将此集合转换为Map,如果键不可哈希,则抛弃。

  1. toMap(): Map<number, T>

继承自

Collection#toMap

注意:这和Map(this.toKeyedSeq())等效,为了能够方便的进行链式调用而提供。

toOrderedMap()

将此集合转换为Map,保留索引的顺序。

  1. toOrderedMap(): OrderedMap<number, T>

继承自

Collection#toOrderedMap

注意:这和OrderedMap(this.toKeyedSeq())等效,为了能够方便的进行链式调用而提供。

toSet()

将此集合转换为Set,如果值不可哈希,则抛弃。

  1. toSet(): Set<T>

继承自

Collection#toSet

注意:这和Set(this)等效,为了能够方便的进行链式调用而提供。

toOrderSet()

将此集合转换为Set,保留索引的顺序。

  1. toOrderedSet(): OrderedSet<T>

继承自

Collection#toOrderedSet

注意:这和OrderedSet(this.valueSeq())等效,为了能够方便的进行链式调用而提供。

toList()

将此集合转换为List,丢弃键值。

  1. toList(): List<T>

继承自

Collection#toList

此方法和List(collection)类似,为了能够方便的进行链式调用而提供。然而,当在Map或者其他有键的集合上调用时,collection.toList()会丢弃键值,同时创建一个只有值的list,而List(collection)使用传入的元组创建list。

  1. const { Map, List } = require('immutable')
  2. var myMap = Map({ a: 'Apple', b: 'Banana' })
  3. List(myMap) // List [ [ "a", "Apple" ], [ "b", "Banana" ] ]
  4. myMap.toList() // List [ "Apple", "Banana" ]
toStack()

将此集合转换为Stack,丢弃键值,抛弃不可哈希的值。

  1. toStack(): Stack<T>

注意:这和Stack(this)等效,为了能够方便的进行链式调用而提供。

迭代器
keys()

一个关于Collection键的迭代器。

  1. keys(): IterableIterator<number>

继承自

Collection#keys

注意:此方法将返回ES6规范的迭代器,并不支持Immutable.js的sequence算法,你可以尝试使用keySeq来满足需求。

values()

一个关于Collection值的迭代器。

  1. values(): IterableIterator<T>

继承自

Collection#values

注意:此方法将返回ES6规范的迭代器,并不支持Immutable.js的sequence算法,你可以尝试使用valueSeq来满足需求。

entries()

一个关于Collection条目的迭代器,是[ key, value ]这样的元组数据。

  1. entries(): IterableIterator<[number, T]>

继承自

Collection#entries

注意:此方法将返回ES6规范的迭代器,并不支持Immutable.js的sequence算法,你可以尝试使用entrySeq来满足需求。

集合(Seq)
keySeq()

返回一个新的Seq.Indexed,其包含这个集合的键值。

  1. keySeq(): Seq.Indexed<number>

继承自

Collection#keySeq

valueSeq()

返回一个新的Seq.Indexed,其包含这个集合的所有值。

  1. valueSeq(): Seq.Indexed<T>

继承自

Collection#valueSeq

entrySeq()

返回一个新的Seq.Indexed,其为[key, value]这样的元组。

  1. entrySeq(): Seq.Indexed<[number, T]>

继承自

Collection#entrySeq

副作用
forEach()

sideEffect将会对集合上每个元素执行。

  1. forEach(
  2. sideEffect: (value: T, key: number, iter: this) => any,
  3. context?: any
  4. ): number

继承自

Collection#forEach

Array#forEach不同,任意一个sideEffect返回false都会停止循环。函数将返回所有参与循环的元素(包括最后一个返回false的那个)。

创建子集
slice()

返回一个新的相同类型的相当于原集合指定范围的元素集合,包含开始索引但不包含结束索引位置的值。

  1. slice(begin?: number, end?: number): this

继承自

Collection#slice

如果起始值为负,那么表示从集合结束开始查找。例如slice(-2)返回集合最后两个元素。如果没有提供,那么新的集合将会从最开始那个元素开始。

如果终止值为负,表示从集合结束开始查找。例如silice(0, -1)返回除集合最后一个元素外所有元素。如果没提供,新的集合将会包含到原集合最后一个元素。

如果请求的子集与原集合相等,那么将会返回原集合。

rest()

返回一个不包含原集合第一个元素的新的同类型的集合。

  1. rest(): this

继承自

Collection#rest

butLast()

返回一个不包含原集合最后一个元素的新的同类型的集合。

  1. butLast(): this

继承自

Collection#butLast

skip()

返回一个不包含原集合从头开始amount个数元素的新的同类型集合。

  1. skip(amount: number): this

继承自

Collection#skip

skipLast()

返回一个不包含原集合从结尾开始amount个数元素的新的同类型集合。

  1. skipLast(amount: number): this

继承自

Collection#skipLast

skipWhile()

返回一个原集合从predicate返回false那个元素开始的新的同类型集合。

  1. skipWhile(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any
  4. ): this

继承自

Collection#skipWhile

  1. const { List } = require('immutable')
  2. List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  3. .skipWhile(x => x.match(/g/))
  4. // List [ "cat", "hat", "god" ]
skipUntil()

返回一个原集合从predicate返回true那个元素开始的新的同类型集合。

  1. skipUntil(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any
  4. ): this

继承自

Collection#skipUntil

  1. const { List } = require('immutable')
  2. List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  3. .skipUntil(x => x.match(/hat/))
  4. // List [ "hat", "god"" ]
take()

返回一个包含原集合从头开始的amount个元素的新的同类型集合。

  1. take(amount: number): this

继承自

Collection#take

takeLast()

返回一个包含从原集合结尾开始的amount个元素的新的同类型集合。

  1. takeLast(amount: number): this

继承自

Collection#take

takeWhile()

返回一个包含原集合从头开始的prediacte返回true的那些元素的新的同类型集合。

  1. takeWhile(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any
  4. ): this

继承自

Collection#takeWhile

  1. const { List } = require('immutable')
  2. List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  3. .takeWhile(x => x.match(/o/))
  4. // List [ "dog", "frog" ]
takeUntil()

返回一个包含原集合从头开始的prediacte返回false的那些元素的新的同类型集合。

  1. takeUntil(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any
  4. ): this

继承自

Collection#takeUntil

  1. const { List } = require('immutable')
  2. List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  3. .takeUntil(x => x.match(/at/))
  4. // List [ "dog", "frog" ]
减少值
reduce()

将传入的方法reducer在集合每个元素上调用并传递缩减值,以此来缩减集合的值。

  1. reduce<R>(
  2. reducer: (reduction: R, value: T, key: number, iter: this) => R,
  3. initialReduction: R,
  4. context?: any
  5. ): R
  6. reduce<R>(reducer: (reduction: T | R, value: T, key: number, iter: this) => R): R

继承自

Collection#reduce

Array#reduce

如果initialReduction未提供,那么将会使用集合第一个元素。

reduceRight()

逆向地缩减集合的值(从结尾开始)。

  1. reduceRight<R>(
  2. reducer: (reduction: R, value: T, key: number, iter: this) => R,
  3. initialReduction: R,
  4. context?: any
  5. ): R
  6. reduceRight<R>(
  7. reducer: (reduction: T | R, value: T, key: number, iter: this) => R
  8. ): R

继承自

Collection#reduceRight

注意:与this.reverse().reduce()等效,为了与Array#reduceRight看齐而提供。

every()

当集合中所有元素predicate都判定为true时返回ture。

  1. every(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any
  4. ): boolean

继承自

Collection#every

some()

当集合中任意元素predicate判定为true时返回ture。

  1. some(
  2. predicate: (value: T, key: number, iter: this) => boolean,
  3. context?: any
  4. ): boolean

继承自

Collection#some

join()

将值连接为字符串,并且在每两个值之间插入分割。默认分隔为","

  1. join(separator?: string): string

继承自

Collection#join

isEmpty()

当集合不包含值时返回true。

  1. isEmpty(): boolean

继承自

Collection#isEmpty

对于惰性Seq,isEmpty会对他经常迭代来确定是否为空。至少会迭代一次。

count()

返回集合的大小。

  1. count(): number
  2. count(
  3. predicate: (value: T, key: number, iter: this) => boolean,
  4. context?: any
  5. ): number

继承自

Collection#count

不管此集合是否惰性地确定大小(某些Seq不能),这个方法将总是返回正确的大小。如果必要,他将会评估一个惰性的Seq。

如果predicate提供了,方法返回的数量将是集合中predicate返回true的元素个数。

countBy()

返回Seq.Keyed的数量,由grouper方法将值分组。

  1. countBy<G>(
  2. grouper: (value: T, key: number, iter: this) => G,
  3. context?: any
  4. ): Map<G, number>

注意:这不是一个惰性操作。

对比
isSubset()

如果iter包含集合中所有元素则返回true。

  1. isSubset(iter: Iterable<T>): boolean

继承自

Collection#isSubset

isSuperset()

如果集合包含iter中所有元素则返回true。

  1. isSuperset(iter: Iterable<T>): boolean

继承自

Collection#isSuperset