Saturday, January 4, 2014

How to make Array-splice to take an array of values

Did you ever come to a situation where you want use splice with an array of values?
According to w3schools the definition of splice is


Instead of item1,.....,itemX I'd like to hand over an array of values.
I've used the build in method apply to call splice on the Array prototype directly:

var test = ["1", "4"],
    args = [1, 0, "2", "3"];

Array.prototype.splice.apply(test, args);
console.log(test); // output: [ '1', '2', '3', '4' ]

No comments:

Post a Comment