Skip to content

Instantly share code, notes, and snippets.

@rosd89
Last active April 6, 2019 18:27
Show Gist options
  • Save rosd89/a724a91a7282ff972a2da995f00f0383 to your computer and use it in GitHub Desktop.
Save rosd89/a724a91a7282ff972a2da995f00f0383 to your computer and use it in GitHub Desktop.
const splice = (arr = [], start = 0, deleteCount = 0, ...items) => {
if (start < 0 || arr.length - 1 < start) {
return [[], [...arr, ...items]];
}
const arr1 = [];
const arr2 = [...arr];
const result = [];
for (let i = 0; i < arr.length; i++) {
if (i == start) break;
arr1.push(arr2.shift());
}
if(deleteCount > 0) {
for(let i = deleteCount; i > 0; i--) {
result.push(arr2.shift());
}
}
return [[...result], [...arr1, ...items, ...arr2]];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment