在另一个数组中插入一个数组的项。

时间:2021-05-25 21:29:05

How to insert an array's items inside another array?

如何在另一个数组中插入数组的项?

Inside means inserting the array and "expanding" it for its items to become part of the array in which they were inserted. Example:

内部意味着插入数组并将其“展开”,使其成为插入它们的数组的一部分。例子:

var mainArr=[a,e,f,g];
var innerArr=[b,c,d];

with the resulting array being:

结果数组为:

[a,b,c,d,e,f,g]

The order of the inserted items should be retained. Assume the items are not sortable.

应保留插入项的顺序。假设这些项不是可排序的。

If the ExtJS framework facilitates the above via its Array functions those can be utilized in this scenario.

如果ExtJS框架通过其数组函数方便了上述操作,那么可以在此场景中使用这些函数。

3 个解决方案

#1


3  

If you know the index to "insert" at, you can use concat:

如果你知道“插入”的索引,你可以使用concat:

var result = mainArr.slice(0,2).concat(innerArr, mainArr.slice(2));

You could also just concat the two arrays together and then sort them with .sort(function (a, b) { return a - b; }); if they contain only numbers that are to be in order.

你也可以把两个数组合并在一起,然后用。sort(函数(a, b) {return a - b;});如果它们只包含要按顺序排列的数字。


EDIT: I was not going to suggest .splice because it takes scalar arguments, but thanks to the magic of .apply you can still do it:

编辑:我不打算建议。splice,因为它需要标量参数,但是多亏了。apply你仍然可以这样做:

Array.prototype.splice.apply(mainArr, [index,0].concat(innerArr));

#2


2  

If you want to insert the array inside at a certain index:

如果你想把数组插入到某个索引中:

mainArr.splice.apply(mainArr,[index,0].concat(innerArr))

will insert the innerArr inside the main Arr at the specified index.

在指定索引处插入主Arr内的innerArr。

This will run splice on mainArr with the arguments index,0,...innerArr contents... and add those extra arguments at the specified index.

这将在mainArr上使用参数索引,0,…innerArr内容……并在指定索引处添加这些额外的参数。

Update

The one liner modifies mainArr, it doesn't return the value.

其中一行修改了mainArr,它没有返回值。

If you need to preserve mainArr, you can clone it

如果你需要保护mainArr,你可以克隆它。

newarr = mainArr.splice(0)
newarr.splice.apply(newArr,[index,0].concat(innerArr))

or (less readable)

或(更少的可读)

mainArr.splice(0).splice.apply(this,[index,0].concat(innerArr))

#3


1  

You can use this :

你可以用这个:

var mainArr=['a','e','f','g'];
var innerArr=['b','c','d'];
mainArr=mainArr.concat(innerArr).sort()
alert(mainArr)

http://jsfiddle.net/fR2cC/3/

http://jsfiddle.net/fR2cC/3/

#1


3  

If you know the index to "insert" at, you can use concat:

如果你知道“插入”的索引,你可以使用concat:

var result = mainArr.slice(0,2).concat(innerArr, mainArr.slice(2));

You could also just concat the two arrays together and then sort them with .sort(function (a, b) { return a - b; }); if they contain only numbers that are to be in order.

你也可以把两个数组合并在一起,然后用。sort(函数(a, b) {return a - b;});如果它们只包含要按顺序排列的数字。


EDIT: I was not going to suggest .splice because it takes scalar arguments, but thanks to the magic of .apply you can still do it:

编辑:我不打算建议。splice,因为它需要标量参数,但是多亏了。apply你仍然可以这样做:

Array.prototype.splice.apply(mainArr, [index,0].concat(innerArr));

#2


2  

If you want to insert the array inside at a certain index:

如果你想把数组插入到某个索引中:

mainArr.splice.apply(mainArr,[index,0].concat(innerArr))

will insert the innerArr inside the main Arr at the specified index.

在指定索引处插入主Arr内的innerArr。

This will run splice on mainArr with the arguments index,0,...innerArr contents... and add those extra arguments at the specified index.

这将在mainArr上使用参数索引,0,…innerArr内容……并在指定索引处添加这些额外的参数。

Update

The one liner modifies mainArr, it doesn't return the value.

其中一行修改了mainArr,它没有返回值。

If you need to preserve mainArr, you can clone it

如果你需要保护mainArr,你可以克隆它。

newarr = mainArr.splice(0)
newarr.splice.apply(newArr,[index,0].concat(innerArr))

or (less readable)

或(更少的可读)

mainArr.splice(0).splice.apply(this,[index,0].concat(innerArr))

#3


1  

You can use this :

你可以用这个:

var mainArr=['a','e','f','g'];
var innerArr=['b','c','d'];
mainArr=mainArr.concat(innerArr).sort()
alert(mainArr)

http://jsfiddle.net/fR2cC/3/

http://jsfiddle.net/fR2cC/3/