1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| function insert(arr, item, index) { var newArr = []; for (var key in arr) { if (key == index) { newArr.push(item); newArr.push(arr[key]); } else { newArr.push(arr[key]); } } return !newArr.length ? [item] : newArr; }
function insert1(arr, item, index) { return arr.slice(0,index).concat(item,arr.slice(index)); }
function insert2(arr, item, index) { var newArr=arr.concat(); newArr.splice(index,0,item); return newArr; }
function insert3(arr, item, index) { var newArr=arr.slice(0); newArr.splice(index,0,item); return newArr; }
function insert4(arr, item, index) { var newArr=[]; [].push.apply(newArr, arr); newArr.splice(index,0,item); return newArr; }
function insert5(arr, item, index) { var newArr=[]; for(var i=0;i<arr.length;i++){ newArr.push(arr[i]); } newArr.splice(index,0,item); return newArr; }
|