DEV Community

Randy Rivera
Randy Rivera

Posted on

Removing Items Using splice()

  • What if we want to remove an element from somewhere in the middle? Or remove more than one element at once? Well, that's where splice() comes in. splice() allows us to do just that: remove any number of consecutive elements from anywhere in an array.
  • splice() can take up to 3 parameters, but for now, we'll focus on just the first 2. The first two parameters of splice() are integers which represent indexes, or positions, of the array that splice() is being called upon. And remember, arrays are zero-indexed, so to indicate the first element of an array, we would use 0. splice()'s first parameter represents the index on the array from which to begin removing elements, while the second parameter indicates the number of elements to delete
  • Ex: We've initialized an arrayarr. Let's use splice() to remove elements from arr, so that it only contains elements that sum to the value of 10.
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
arr.splice(1, 4);
Enter fullscreen mode Exit fullscreen mode
console.log(arr); will display [2, 5, 2, 1]
Enter fullscreen mode Exit fullscreen mode
  • Here we removed 4 elements, beginning with the second element (at index 1). arr would have the value [2, 5, 2, 1] which sum to the value of 10 respectfully.

Top comments (0)