468,283 Members | 2,005 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,283 developers. It's quick & easy.

splice an array into another array

MSDN says splice has the following arguments:

arrayObj.splice(start, deleteCount, [item1[, item2[, . . .
[,itemN]]]])

Thus I can insert items into an array using splice. But how do I
insert an entire array? For example:

var arr1 = [];
arr1[0] = "a";
arr2[1] = "d";
var arr2 = [];
arr2[0] = "b";
arr2[1] = "c";
arr1.splice(1, 0, arr2);

Because array arguments in splice dont work like they do in concat,
this expectedly yeilds this for arr1: a,[b,c],d

Without having to use slice and concat which allocates up to three new
arrays, is there anyway to splice one array into another?

Thanks
Jun 27 '08 #1
1 2759
<ag*******@gmail.comwrote:
MSDN says splice has the following arguments:

arrayObj.splice(start, deleteCount, [item1[, item2[, . . .
[,itemN]]]])

Thus I can insert items into an array using splice. But
how do I insert an entire array? For example:

var arr1 = [];
arr1[0] = "a";
arr2[1] = "d";
var arr2 = [];
arr2[0] = "b";
arr2[1] = "c";
arr1.splice(1, 0, arr2);

Because array arguments in splice dont work like they do in
concat, this expectedly yeilds this for arr1: a,[b,c],d

Without having to use slice and concat which allocates up to
three new arrays, is there anyway to splice one array into
another?
I suppose something like:-

arr2.unshift(1, 0); //Adding the start and deleteCount
//to the front of arr2.
arr1.splice.apply(arr1, arr2); //Using the resulting array
// as the array of arguments
// for - apply -.

- shoud do it. Though if you still wanted to use arr2 as it was you
would have to shift the first two values out of it again.

Richard.

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gary N. | last post: by
3 posts views Thread by DeanJo | last post: by
1 post views Thread by fishctr | last post: by
18 posts views Thread by dhtml | last post: by
1 post views Thread by magarwal | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.