Skip to content

Instantly share code, notes, and snippets.



Last active Feb 22, 2019
What would you like to do?
An ES6 / pure function implementation of the JS splice method that returns an object of the new array and the removed elements
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)

This comment has been minimized.

Copy link

@filippoitaliano filippoitaliano commented Sep 15, 2017

This function is an 'inpure function', isn't it? It modifies the array passed as parameter.


This comment has been minimized.

Copy link

@sergiubologa sergiubologa commented Feb 21, 2018

Yes, it's not really a pure function because it modifies the array.


This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment