Given an array with some elements, the task is to remove multiple elements from end of an array in JavaScript. This article cover various approaches to remove multiple items from array with explanation and code examples.
Examples
Input: Arrr = [ 10, 20, 30, 40, 50 ]
itemRemove = 2
Output: [ 10, 20, 30 ]
Input: Arr = [ 'Apple', 'Banana', 'Cat', 'Dog' ]
itemRemove = 1
Output: [ 'Apple', 'Banana', 'Cat' ]
Array.slice() Method to Remove Multiple Items from End of an Array
The slice() method is used to create a new array excluding the specified number of items from the end. The below code removes the last two items from the array Arr using the slice() method. We will create a new array newArr that excludes the last two elements.
const Arr = [ 10, 20, 30, 40, 50, 60 ]; const itemsToRemove = 2; const newArr = Arr.slice(0, -itemsToRemove); // Output: [ 10, 20, 30, 40 ] console.log(newArr);
Array.splice() Method to Remove Multiple Items from End of an Array
The splice() method is used to remove the specified number of items from the end. The code removes the last two items from the array Arr using the splice() method. This method directly modify original array Arr to exclude the last two elements.
const Arr = [ 10, 20, 30, 40, 50, 60 ]; const itemsToRemove = 2; Arr.splice(-itemsToRemove); // Output: [ 10, 20, 30, 40 ] console.log(Arr);
Array.filter() Method to Remove Multiple Items from End of an Array
The filter() method is used to create a new array excluding the specified number of items from the end. The code removes the last two items from the array Arr using the filter() method. It creates a new array newArr that includes only the first four elements.
const Arr = [ 10, 20, 30, 40, 50, 60 ]; const itemsToRemove = 2; const newArr = Arr.filter((_, index, arr) => index < arr.length - itemsToRemove); // Output: [ 10, 20, 30, 40 ] console.log(newArr);
Array.length Property to Remove Multiple Items from End of an Array
The length property of the array to truncate it and remove the specified number of items from the end. The code shortens the array Arr by reducing its length, removing the last two items. This modifies Arr to exclude the last two elements.
const Arr = [ 10, 20, 30, 40, 50, 60 ]; const itemsToRemove = 2; Arr.length = Arr.length - itemsToRemove; // Output: [ 10, 20, 30, 40 ] console.log(Arr);
ES6 Destructuring to Remove Multiple Items from End of an Array
Using ES6 array destructuring to create a new array without the specified number of items from the end. The code removes the last two items from the array Arr using array destructuring and reversing. It creates a new array newArr with the remaining elements.
const Arr = [ 10, 20, 30, 40, 50, 60 ]; const itemsToRemove = 2; const [, , ...newArr] = Arr.reverse(); // Output: [ 10, 20, 30, 40 ] console.log(newArr.reverse());
For Loop to Remove Multiple Items from End of an Array
The code removes the last two items from the array Arr by using a for loop to copy all but the last two elements into a new array newArr.
const Arr = [ 10, 20, 30, 40, 50, 60 ]; const itemsToRemove = 2; const newArr = []; for (let i = 0; i < Arr.length - itemsToRemove; i++) { newArr.push(Arr[i]); } // Output: [ 10, 20, 30, 40 ] console.log(newArr);