There doesn't seem to be a way to extend an existing JavaScript array with another array, ie to emulate Python's extend
method.(似乎没有办法用另一个数组扩展现有的JavaScript数组,即模拟Python的extend
方法。)
I want to achieve the following:(我想实现以下目标:)
>>> a = [1, 2]
[1, 2]
>>> b = [3, 4, 5]
[3, 4, 5]
>>> SOMETHING HERE
>>> a
[1, 2, 3, 4, 5]
I know there's a a.concat(b)
method, but it creates a new array instead of simply extending the first one.(我知道有一个a.concat(b)
方法,但它创建了一个新数组,而不是简单地扩展第一个数组。)
I'd like an algorithm that works efficiently when a
is significantly larger than b
(ie one that does not copy a
).(我想要一个算法,当a
明显大于b
(即不复制a
的算法),它可以有效地工作。)
Note: This is not a duplicate of How to append something to an array?(注意:这不是如何将某些内容附加到数组的副本?)
-- the goal here is to add the whole contents of one array to the other, and to do it "in place", ie without copying all elements of the extended array.( - 这里的目标是将一个数组的全部内容添加到另一个数组中,并“就地”执行,即不复制扩展数组的所有元素。)
ask by DzinX translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…