The splice syntax is:

array.splice(start, deleteCount[, item1[, item2[, ...]]]) 

I have an array of item1, item2, .... How do I insert it into this function?

ADDED: That is, something like this: array.splice(1, 1, ["a","b"])

  • Added a question - ivan0biwan

1 answer 1

A spread operator has been added to ES2015, it allows you to expand an array into an argument list

 var array = [1, 2, 3]; array.splice(1, 1, ...["a", "b"]) console.log(array); 

Previously would have to use the apply method

 var array = [1, 2, 3]; array.splice.apply(array, [1, 1].concat(["a", "b"])); // если все известно заранее можно сразу составить нужный массив [1,1,"a","b"] console.log(array);