在 ImmutableJS 中压缩多个列表的最佳方法是什么
what is the best way to zip multiple lists in ImmutableJS
假设我有一个列表列表。例如:
const l : List<List<number>> = fromJS([[0,1,2,3],[4,5,6,7],[8,9,10,11]])
压缩 "l" 的最佳方法是什么(不使用 toJS())所以我会得到:
[[0,4,8],[1,5,9],[2,6,10],[3,7,11]]
我相信你想使用 List#zip。
const l = Immutable.fromJS([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]
]);
const zipped = l.get(0).zip(...l.rest());
console.log(zipped);
// [ [0,4,8], [1,5,9], [2,6,10], [3,7,11] ];
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.9/immutable.js"></script>
请注意,此 returns 数组列表。不过,将它们变成列表很容易:
const zippedLists = zipped.map(List);
如果您要压缩不同大小的列表,您可能还对 List#zipAll 感兴趣。
假设我有一个列表列表。例如:
const l : List<List<number>> = fromJS([[0,1,2,3],[4,5,6,7],[8,9,10,11]])
压缩 "l" 的最佳方法是什么(不使用 toJS())所以我会得到:
[[0,4,8],[1,5,9],[2,6,10],[3,7,11]]
我相信你想使用 List#zip。
const l = Immutable.fromJS([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]
]);
const zipped = l.get(0).zip(...l.rest());
console.log(zipped);
// [ [0,4,8], [1,5,9], [2,6,10], [3,7,11] ];
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.9/immutable.js"></script>
请注意,此 returns 数组列表。不过,将它们变成列表很容易:
const zippedLists = zipped.map(List);
如果您要压缩不同大小的列表,您可能还对 List#zipAll 感兴趣。