How to extend an existing JavaScript array with another array, without creating a new array

后端 未结 16 2048
深忆病人
深忆病人 2020-11-22 07:38

There doesn\'t seem to be a way to extend an existing JavaScript array with another array, i.e. to emulate Python\'s extend method.

I want to achieve th

16条回答
  •  轮回少年
    2020-11-22 08:01

    Super simple, does not count on spread operators or apply, if that's an issue.

    b.map(x => a.push(x));
    

    After running some performance tests on this, it's terribly slow, but answers the question in regards to not creating a new array. Concat is significantly faster, even jQuery's $.merge() whoops it.

    https://jsperf.com/merge-arrays19b/1

提交回复
热议问题