Skip to content

Instantly share code, notes, and snippets.

@gemfarmer
Created October 5, 2013 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gemfarmer/6845984 to your computer and use it in GitHub Desktop.
Save gemfarmer/6845984 to your computer and use it in GitHub Desktop.
The same as .splice(), but without side effects. "replace" can be several items and the funciton will still work cleanly
var spliceClean = function(arr, start, numToReplace, replace) {
var end = (start+numToReplace-1)
var newArray = [];
for (i=0; i<arr.length; i++){
(i < start) ? newArray.push(arr[i]) : console.log("rejected")
}
for (i=3; i<arguments.length; i++){
newArray.push(arguments[i])
}
for (i=0; i<arr.length; i++){
(end< i) ? newArray.push(arr[i]) : console.log("rejected")
}
return newArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment