If you are working with arrays in JavaScript. So that, there is a very frequently asked question. Which last 2, 3, 4..n elements should be removed from the array in js.
In this tutorial, we will show you some approaches on how to remove the last 2, 3, 4, …N elements from an array in javascript.
How to Remove the Last 2,3,4,N Elements from an Array in JavaScript
Here are some unique approaches to remove the last 2, 3, 4, …N elements from array in js:
- Approach 1: Using
slice()
- Approach 2: Using
splice()
- Approach 3: Remove Last N Elements From an Array in JavaScript Using the for Loop and pop()
Approach 1: Using slice()
The slice()
method helps us to create a new array by removing elements from an existing array within a specific range.
Here is an example of removing the last 2 elements from an array:
// Example array
let myArray = [1, 2, 3, 4, 5, 6, 7];
// Specify the number of elements to remove from the end
let elementsToRemove = 3;
// Create a new array without the last elements
let newArray = myArray.slice(0, -elementsToRemove);
console.log(`Array after removing the last ${elementsToRemove} elements:`, newArray);
Here is another example of removing the last n elements from an array:
// Example array
let myArray = [1, 2, 3, 4, 5, 6, 7];
// Specify how many elements to remove from the end
let elementsToRemove = 3;
// Create a new array without the last elements
let newArray = myArray.slice(0, -elementsToRemove);
console.log(`Array after removing the last ${elementsToRemove} elements:`, newArray);
Approach 2: Using splice()
The splice()
method modifies the elements of an array by removing or replacing existing elements.
Here is an example of removing last 2 elements from an array:
// Example array
let myArray = [1, 2, 3, 4, 5, 6, 7];
// Specify the number of elements to remove from the end
let elementsToRemove = 3;
// Remove the last elements using splice
myArray.splice(-elementsToRemove);
console.log(`Array after removing the last ${elementsToRemove} elements:`, myArray);
Here is another example of removing the last n elements from an array:
function removeLastNElementsInPlace(arr, n) {
// Remove the last n elements from the original array
arr.splice(-n);
}
// Example usage:
let myArray = [1, 2, 3, 4, 5, 6, 7];
removeLastNElementsInPlace(myArray, 3);
console.log("Array after removing the last 3 elements:", myArray);
Approach 3: Remove Last N Elements From an Array in JavaScript Using the for Loop and pop()
You can use the pop()
method to remove the last n element from an array in JavaScript. Here’s an example demonstrating how to remove the last 2, 3, or n elements using the pop()
method:
function removeLastNElementsWithPop(arr, n) {
// Remove the last n elements using pop() in a loop
for (let i = 0; i < n; i++) {
arr.pop();
}
}
// Example usage:
let myArray = [1, 2, 3, 4, 5, 6, 7];
removeLastNElementsWithPop(myArray, 3);
console.log("Array after removing the last 3 elements:", myArray);
The next example using for loop and pop to remove the last N elements from an array.
function removeLastNElementsWithPop(arr, n) {
// Remove the last N elements using pop() in a loop
for (let i = 0; i < n; i++) {
arr.pop();
}
}
// Example usage:
let myArray = [1, 2, 3, 4, 5, 6, 7];
removeLastNElementsWithPop(myArray, 3);
console.log("Array after removing the last 3 elements:", myArray);
Conclusion
In JavaScript, taking out the last 2,3,4, N elements/items from an array can be done in various ways, such as using slice(), splice(), or pop(). We hope this guide has helped you understand how to remove last 2, 3, 4 N elements in a JavaScript array