使用Javascript - 插入另一数组中的数组 [英] Javascript - insert an array inside another array

查看:131
本文介绍了使用Javascript - 插入另一数组中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是插入另一个数组中的数组的更有效的方式。

What is the more efficient way to insert an array inside another array.

a1 = [1,2,3,4,5];
a2 = [21,22];

newArray - a1.insertAt(2,a2) -> [1,2, 21,22, 3,4,5];

迭代A2使用拼接看起来有点从性能上看awfull如果a2数组是很大的。

Iterating a2 using splice looks a bit awfull from a performance point of view if a2 array is large.

感谢。

推荐答案

您可以使用拼接一些适用挂羊头卖狗肉:

You can use splice combined with some apply trickery:

a1 = [1,2,3,4,5];
a2 = [21,22];

a1.splice.apply(a1, [2, 0].concat(a2));

console.log(a1); // [1, 2, 21, 22, 3, 4, 5];

这篇关于使用Javascript - 插入另一数组中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆