如何将数组与 immutable.js 中的列表合并

How to merge an array with a list in immutable.js

我有一个列表和一个数组。我想通过偏移量合并它们。

list = List([1, 2, 3, 4, 5]);
arr = [7, 8, 9];

// I expect some operation like this to make the list to be [1, 2, 7, 8, 9]:
list = list.merge(arr, 2)

不知道怎么处理。非常感谢。

你找的是以下两种方法:

  • Collection.slice() 在您的示例中会将列表从 0 剪切到 2,从而有效抵消合并。
  • List.concat() 然后可用于将数组合并到列表中。

然后您可以将它们链接在一起,像这样产生所需的结果:

list = Immutable.List([1, 2, 3, 4, 5]);
arr = [7, 8, 9];

list = list.slice(0, 2).concat(arr);
console.log(list)
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script>