Remove portion of an array in JavaScript
This post will discuss how to remove a portion of an array in JavaScript.
There are several ways to remove a portion from an array in JavaScript. We need to remove a subarray of an array that can be specified by a start and an end index. Here are some of the most common functions:
1. Using Array.splice() function
The Array.splice() function removes and/or inserts elements to/from an array, and returns the removed elements as a new array. The original array is modified by this function. To remove a portion of an array, we need to pass the start index and the number of elements to remove as arguments to the splice() function. Here’s an example:
|
1 2 3 4 5 6 7 |
var array = ["a", "b", "c", "d", "e"]; // remove from index 1, and remove 3 elements var removed = array.splice(1, 3); console.log(array); // ["a", "e"] console.log(removed); // ["b", "c", "d"] |
2. Using Array.slice() function
The Array.slice() function returns a shallow copy of a portion of an array as a new array, without modifying the original array. To remove a portion of an array, we need to use the slice() function twice: once to get the elements before the slice, and once to get the elements after the slice. Then, we can concatenate the two arrays using the Array.concat() function. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var array = ["a", "b", "c", "d", "e"]; // get the elements before index 1 var before = array.slice(0, 1); // get the elements after index 4 var after = array.slice(4); // concatenate the two arrays var newArray = before.concat(after); console.log(array); // ["a", "b", "c", "d", "e"] console.log(newArray); // ["a", "e"] |
3. Using Array.filter() function
The Array.filter() function creates a new array with all elements that pass the test implemented by the provided function. To use it, we need to pass a function that returns true or false based on some condition. We can then assign the original array to the filtered array to effectively remove the unwanted portion. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 |
var array = ["a", "b", "c", "d", "e"]; // get the elements before index 1 and after index 4 var filteredArray = array.filter((val, index) => { if (index < 1 || index > 3) { return val; } }); console.log(array); // ["a", "b", "c", "d", "e"] console.log(filteredArray); // ["a", "e"] |
4. Using Array.push() function
The Array.push() function adds one or more elements to the end of an array and returns the new length of the array. To remove a portion of an array, we need to iterate over the array and push only the elements that are not in the portion to a new array. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var array = ["a", "b", "c", "d", "e"]; var newArray = []; for (var i = 0; i < array.length; i++) { // skip the elements from index 1 to index 3 if (i < 1 || i > 3) { newArray.push(array[i]); } } console.log(array); // ["a", "b", "c", "d", "e"] console.log(newArray); // ["a", "e"] |
That’s all about removing a portion of 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 :)