Remove a range of elements from an array in JavaScript
This post will discuss how to remove a range of elements from an array in JavaScript.
A range of elements is a contiguous sequence of elements that have consecutive indexes in the array. Here are some examples of how to remove a range of elements from an array in JavaScript given an array and a start and end index:
1. Using splice() function
We can use splice() function to remove a range of elements from an array by passing the start index and the delete count to the function. The delete count is the number of elements to be removed, which can be calculated by subtracting the start index from the end index and adding one. The splice() function returns a new array containing the deleted elements, and modifies the original array in place. For example, if we want to remove the range of elements from index 1 to index 3 (inclusive), we can do:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
let arr = [1, 2, 3, 4, 5]; let start = 1; // the start index let end = 3; // the end index // the number of elements to be removed let deleteCount = end - start + 1; // remove the range of elements let removed = arr.splice(start, deleteCount); console.log(arr); // [1, 5] console.log(removed); // [2, 3, 4] |
2. Using filter() function
This is a another built-in function that creates a new array with all elements that pass a condition implemented by a specified function. We can use this function to remove a range of elements from an array by using a function that returns false for the indexes that we want to remove, and true for the rest. For example, if we want to create a new array without the range of elements from index 1 to index 3 (inclusive), we can do:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
let arr = [1, 2, 3, 4, 5]; let start = 1; // the start index let end = 3; // the end index // filter out the elements with index 1, 2, or 3 let filtered = arr.filter(function(element, index) { // Return true if the index is less than start or greater than end return index < start || index > end; }); console.log(arr); // [1, 2, 3, 4, 5] console.log(filtered); // [1, 5] |
The filter() function is a simple and concise way to remove a range of elements from an array in JavaScript, but it does not change the original array, but returns a new array with the filtered elements. We can also use a for loop to iterate over the elements of the original array and copy only those that are outside the range to a new array.
3. Using slice() function
The slice() function is another built-in function that returns a shallow copy of a portion of an array, without modifying the original array. This function takes two arguments: the start index and the end index (exclusive) of the slice. To remove a range of elements from an array using this function, we can use the slice() function twice to get the parts before and after the range and then concatenate them using the concat() function or the spread operator (…).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
let arr = [1, 2, 3, 4, 5]; let start = 1; // the start index let end = 3; // the end index // create a new array without the range of elements let newArr = arr.slice(0, start).concat(arr.slice(end + 1)); // or use spread operator // let newArr = [...arr.slice(0, start), ...arr.slice(end + 1)]; console.log(arr); // [1, 2, 3, 4, 5] console.log(newArr); // [1, 5] |
Similar to the filter() function, the slice() function does not change the original array, but returns a new array with the sliced elements. That’s all about removing a range of elements from an array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)