Remove The First N Elements From An Array Using JavaScript

Remove the first N elements from an Array using JavaScript

You will know how to remove the first N elements from an array using JavaScript through the splice() or loop method. These important skills must be learned if you are working with arrays in Javascript.

Remove the first N elements from an arrray using JavaScript

Using splice()

The splice() method will remove the first N elements in an array starting from a specific index given.

Syntax:

splice(start, count)

Parameters:

  • start: The starting point (index) to begin removing.
  • count: The number of how many elements you want to remove.

You can use the splice() function to remove a number of elements starting from 0 index, which means removing the first N elements from an array.

let array = ['learn', 'share', 'it'];
array.splice(0, 2);
console.log(array);

Output: 

['it']

The above example points out that we have an array of 3 elements (As declared), and we want to remove the first 2 elements from that array. We have used splice() method whose first parameter is index zero (To remove from the beginning) and the second parameter equal to 2 (To remove 2 elements).

Using shift() with a loop

The method shift() will remove the first element (Only one) from an array.

Syntax:

shift()

However, if your array is empty, this method will return undefined instead of an empty array.

let n = 2;
let array = ['learn', 'share', 'it'];

for (i = 1; i <= n; i++) {
    array.shift();
}
console.log(array);

Output: 

['it']

The logic behind this approach is very easy to accept. Because we want to remove 2 elements from an array, we declare our n variable equals to 2 and then use the for loop to repeat the shift() statement for 2 times as this method only removes the first one element from an array. We then receive an array with the first 2 elements having been removed.

Using delete with a loop

The delete statement will remove the value of elements from the array but it will leave an null element at the index deleted.

Syntax:

delete arrayName[index]

Parameter:

  • arrayName: The name of the array.
  • index: The index of an element which you want to delete.

You can use this statement with the help of a for loop (As like in the previous approach) to run the delete array[i] statement for N times:

let n = 2;
let array = ['learn', 'share', 'it'];

for (i = 0; i < n; i++) {
    delete array[i];
}
console.log(array);

Output: 

[null, null, 'it']

As you can see, this method will leave an null element instead of clearing it. However, all the methods we have introduced are the most powerful and optimized. You may see another method that will do the work, but it is not the fastest way.

Summary

We have learned some methods to remove the first N elements from an array using JavaScript. With the use of our guide in this tutorial and others, you can be successful in achieving the task.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *