简体   繁体   English

如何在给定索引下将一个不变列表中的所有元素插入到另一个中

[英]How to insert all elements from one immutable list into another at given index

I need to insert the contents of an array (pure JS) into an immutable List at a given index. 我需要将数组(纯JS)的内容插入到给定索引的不可变列表中。

I have a list like [a,b,c,d,e] which represents the ids at indexes 0 - 4 on the server. 我有一个像[a,b,c,d,e]的列表[a,b,c,d,e]它表示服务器上索引0 - 4的ID。 I then fetch an array [j,k,l,m,n] (which represents the id's at indices 9 - 14 ). 然后,我获取一个数组[j,k,l,m,n] (它表示索引9 - 14处的id)。 I want to merge the two lists so that I have: 我想合并两个列表,以便拥有:

[a,b,c,d,e,undefined,undefined,undefined,undefined,undefined,j,k,l,m,n]

It's possible to do oldList.insert(9, nextList) but this nests the whole nextList array into index 9 and not it's contents at indices 9 - 14 可以执行oldList.insert(9, nextList)但这oldList.insert(9, nextList)整个nextList数组嵌套到索引9而不是其内容位于索引9 - 14

I need something like the ES6/Next spread operator.. 我需要类似ES6 / Next传播算子的东西。

const ids = oldList.insert(10, ...next_ids)

But this isn't possible. 但这是不可能的。

Any ideas? 有任何想法吗?

Here is my current solution but be interested to know if there is a more succinct way.. 这是我当前的解决方案,但有兴趣知道是否还有更简洁的方法。

const list = List( ['a', 'b', 'c', 'd', 'e'] ) // Indices 0 - 5
const fetchResponse = ['j', 'k', 'l', 'm', 'n'] // Indices 9 - 14
const nextList = List()
  .setSize(9) // The start index of fetched results
  .concat(List(fetchResponse))
  .mergeWith((next, last) => next || last, list) // Take new value at index if defined, otherwise old

console.log(nextList.toJS())
  -> ['a', 'b', 'c', 'd', 'e', null, null, null, null, null, 'j', 'k', 'l', 'm', 'n']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Immutable.js将元素从一个列表移动到另一个列表 - Move elements from one List to another with Immutable.js 给定JavaScript中的特定索引,如何将元素从一个数组复制到另一种不同类型的数组? - How can I copy elements from one array to another of a different type given a particular index in JavaScript? 在每个'n'索引之后,将元素从一个数组插入另一个数组 - Insert elements from one array to another after each 'n' index 如何将元素从一个列表移动到另一个列表 - How to move elements from one list to another 如何比较一个数组中的元素与Javascript中另一个数组的索引? - how to compare the elements of one array to the index of another array in Javascript? 不可变的JS从地图中的所有元素中删除属性 - Immutable JS remove property from all elements in map 如果元素本身在另一个数组中,如何在特定索引的数组中插入元素? - How to insert elements in an array at a particular index in case the elements are themselves in another array? PHP-根据另一个插入下拉列表中的数据 - PHP - insert data from dropdown list based on another one 删除所有DOM元素中隐藏的属性,除非使用Javascript函数给出其索引不起作用 - Removing Attribute hidden from All DOM Elements Except That Whose Index is given Using Javascript Function Not Working 如何从列表中选择所有可见元素? - How to select all visible elements from list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM