如何从无序的Immutable.List()中删除重复项?

时间:2016-03-07 20:17:37

标签: immutable.js

如何从无序的Immutable.List()中删除重复项? (不使用toJS()或toArray())

e.g。

Immutable.List.of("green", "blue","green","black", "blue")

2 个答案:

答案 0 :(得分:34)

您可以将其转换为Set。集合是具有唯一值的列表。

Immutable.List.of("green", "blue","green","black", "blue").toSet()

如果您再次需要它,请将其转换回来:

Immutable.List.of("green", "blue","green","black", "blue").toSet().toList()

<强>更新

获得唯一值的可能性较小:

Immutable.List.of("green", "blue","green","black", "blue").distinct

答案 1 :(得分:23)

如果您有更复杂的类型,您还可以使用groupBy提供自己的选择器进行比较。以下内容将删除属性.name上的重复项:

var distinctColors = duplicateColors.groupBy(x => x.name).map(x => x.first()).toList();