How to delete values ​​from an array in Javascript

This is one of the most common questions when working with Javascript. In this short tutorial we will explain how to remove an element from an array using the Javascript programming language. We will explain it in detail in order for you to use this article as a definitive guide for it, from how to eliminate first, final and intermediate elements .

How can I remove an element from an array?

Does the delete operator have any use? There are also other functions with fun names like splice() and slice(), what about those functions?

Read: How to check if a string contains another string in JavaScript

Use splice() to remove a specific value

The correct way to remove an element from an array is to use splice(). We need an index and the number of elements you want to remove, taking that index as a starting point.

var array = [ “a” , “b” , “c” ];

var index = 1 ;

array.splice(index,1); // value b : javascript array splice

// Resulting array = [ “a”, “c”]

Don’t confuse this function with its cousin slice(), which is used to extract parts of an array.

Use shift() and pop() to delete from start to finish

If you are interested in how to eliminate the first or last position of an array in a simple way, you are in luck because there are functions in Javascript that make your work much easier. The shift() and pop() functions exist for precisely this purpose.

Read: Comparisons in Javascript

var array = [ “a” , “b” , “c” ];

array.shift ();

// We will remove the value “a”                                                           [javascript delete from array]

// The resulting array will be array = [“b”, “c”]

var array = [ “a” , “b” , “c” ];

array.pop();                                                                      [ javascript element object from array]

// We will remove the value “c”

// The resulting array will be array= [“a”, “b”].                 [javascript remove element from array]

Use delete to create empty spaces

Whatever you do, don’t use delete to remove an element from an array. The Javascript language specifies that the arrays are sparse, that is, there may be holes between the elements. Using the delete function will create this kind of holes. An element is removed from the array, but the length property is not updated.

var array = [ “a” , “b” , “c” ];

delete array [ 1 ]; // true : javascript delete element from array

// The resulting array will be array: a,, c

array.length // will return 3

Notice that he has left an empty place and the length of the array remains the same as before.

Read: How to convert a JSON object to String in Javascript

Remember this

The next time you’re removing an element from an array, keep the following tips in mind:

For a specific element: array.splice (index, 1)
First element: array.shift ()
Last element: array.pop ()

Never delete because it generates empty spaces (unless you are clearly interested)


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Marianne elanotta

Marianne is a graduate in communication technologies and enjoys sharing the latest technological advances across various fields. Her programming skills include Java OO and Javascript, and she prefers working on open-source operating systems. In her free time, she enjoys playing chess and computer games with her two children.

Leave a Reply