简体   繁体   中英

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

I have a List and an array. I want to merge them by an offset.

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)

I don't know how to deal with it. I really appreciate it.

What you look for are the following two methods:

  • Collection.slice() which will cut the list from 0 to 2 in your example, effectively offsetting the merging.
  • List.concat() which can then be used to merge the array into the list.

You can then chain them together, like this to produce the desired result:

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM