Delete a specific item of an array in Javascript

For training and consulting, write to us at info@xrmforyou.com

JavaScript is something perhaps every programmer have worked. No matter whether you are a front end developer, back end developer or full stack developer, chances are for some requirement or other you must have used JavaScript.

And if it is JavaScript, you must have used arrays. And so is the requirement to delete a specific element of an array.

Let’s say we have a simple array below.

var arr = [1, 2, 3, 4, 5];

There are two methods here. The first and the foremost method is to use splice.

arr.splice(i, 1) will remove an element from i th index. So if I use arr.splice(2,1), it will remove the third element of the array from the array.

So that’s good. However there may be scenario where just want to make the element at index i no longer exist, but the indexes of other element won’t change, you should use the delete keyword.

delete arr[i]

The above code shall delete the element at index i. However the indexes won’t be altered.

This is a very common requirement faced by everyone and hope this helps!

Debajit Dutta

Microsoft Business Solutions MVP


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading