Last active
February 22, 2019 21:34
-
-
Save beaucharman/d0cfc5eec892b4961a57573599889528 to your computer and use it in GitHub Desktop.
An ES6 / pure function implementation of the JS splice method that returns an object of the new array and the removed elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function safeSplice(array, start, deleteCount, ...replace) { | |
const removed = array.splice(start, deleteCount, ...replace) | |
return { | |
array: array, | |
removed: removed, | |
} | |
} | |
/** usage | |
const array = [1, 2, 3, 4, 5, 6] | |
const { array: newArray, removed } = safeSplice(array, 2, 2, 'foo', 'bar') | |
console.log(newArray, removed) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made one
https://gist.github.com/YuCJ/0a42afc1b578b2545195a7b688dcbab6