views:

64

answers:

3
var newlist = list.slice( 0, pos ).concat( tasks ).concat( list.slice( pos ) );

This makes me shudder just looking at it.

+2  A: 

There is a splice method for Array.

Jacob
But that appears to need each parameter listed out. How do I unfurl another array to be that function's extended parameters, then? Or DOES it accept an array parameter to splice together (as opposed to splicing the passed array as only a single item in the new array)?
Hamster
Yikes, that is ugly. You could use the `apply` method of the `splice` function and build the parameter array from the array you want to splice in, but that's exceptionally hideous. I'm thinking you're better off with your version.
Jacob
+1  A: 

If you didn't want to modify the original array, you can shorten yours a little like this:

var newlist = ​list.slice(0,pos).concat(tasks,list.slice(pos));

http://jsfiddle.net/RgYPw/

patrick dw
A: 

Your method is as good as any- you'd have to splice each member of your second array individually.

var list=[1,2,3,4,5,6,7,8,9], tasks= ['a','b','c'], pos=3;


while(tasks.length ) list.splice(pos,0,tasks.pop());

alert(list.join('\n'))

/*  returned value:
1
2
3
a
b
c
4
5
6
7
8
9
*/
kennebec