문제

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

This makes me shudder just looking at it.

도움이 되었습니까?

해결책

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/

다른 팁

There is a splice method for Array.

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
*/
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top