How to remove n elements from the end of a given array in JavaScript ?
In this article, we will learn how to remove the last n elements from the end of the given array in JavaScript.
We can remove n elements from the end of a given array in the following ways:
It is used to modify an array by adding or removing elements from it. This method accepts the index from which the modification has to be made and the number of elements to delete. The index from which the deletion has to start can be found by subtracting the number of elements from the length of the array.
Syntax:
array.splice(start, deleteCount);
Example: In this example, we will be using the JavaScript splice() method to remove the elements from the end of a given array.
Javascript
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log( "Original Array:" , arr);
let elemsToDelete = 3;
arr.splice(arr.length - elemsToDelete,
elemsToDelete);
console.log( "Modified Array:" , arr);
|
Output
Original Array: [
1, 2, 3, 4, 5,
6, 7, 8, 9
]
Modified Array: [ 1, 2, 3, 4, 5, 6 ]
It is used to remove the last element from the array. This can be repeated in a loop of n iterations to remove the last n elements of the array using the while loop.
Syntax:
array.pop();
Example: In this example, we will be using the Javascript pop() method to remove the elements from the end of a given array.
Javascript
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log( "Original Array:" , arr);
let elemsToDelete = 5;
while (elemsToDelete--)
arr.pop();
console.log( "Modified Array:" , arr);
|
Output
Original Array: [
1, 2, 3, 4, 5,
6, 7, 8, 9
]
Modified Array: [ 1, 2, 3, 4 ]
It is used to filter the array and apply the callback function to each item of the array and filter the element which returns true against the callback function.
Syntax:
Array.filter( callback )
Example: In this example, we will be using the Javascript filter() method to remove the elements from the end of a given array.
Javascript
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log( "Original Array:" , arr);
let elemsToDelete = 5;
let k = arr.filter((x, i) =>
i + elemsToDelete < arr.length)
console.log( "Modified Array:" , k);
|
Output
Original Array: [
1, 2, 3, 4, 5,
6, 7, 8, 9
]
Modified Array: [ 1, 2, 3, 4 ]
This method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged.
Syntax:
arr.slice(begin, end);
Example: In this example, we are using Array slice() method.
Javascript
const arr = [1, 2, 3, 4, 5, 6];
const withoutLast = arr.slice(0, -1);
console.log(arr);
console.log(withoutLast);
|
Output
[ 1, 2, 3, 4, 5, 6 ]
[ 1, 2, 3, 4, 5 ]
By using the while loop we can iterate over the array and we will use pop() method for removing the element from the end.
Example: In this example, we are using while loop.
Javascript
const arr = [1, 2, 3, 4, 5, 6];
let n = 3;
while (n > 0) {
n -= 1;
arr.pop();
}
console.log(arr);
|
Last Updated :
15 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...