How to remove a specific item from an array?

Vihan Pamudya Gammanpila
3 min readJul 14, 2022

--

by Vihan Pamudya / July 15, 2022

Definition:

JavaScript arrays allow you to group values and iterate over them. You can add and remove array elements in different ways. Unfortunately, there is not a simple Array.remove method. There are 9 ways in total (use any of these which suits you). Instead of a delete method, the JavaScript array has a variety of ways you can clean array values.

There are different methods and techniques you can use to remove elements from JavaScript arrays:

you can use it to remove elements from JavaScript arrays in these ways:

  • pop — Removes from the End of an Array
  • shift — Removes from the beginning of an Array
  • splice — removes from a specific Array index
  • filter — allows you to programmatically remove elements from an Array

Method 1: Removing Elements from the Beginning of a JavaScript Array

var ar = ['zero', 'one', 'two', 'three'];

ar.shift(); // returns "zero"

console.log( ar ); // ["one", "two", "three"]

Method 2: Removing Elements from the End of a JavaScript Array

var ar = [1, 2, 3, 4, 5, 6];

ar.length = 4; // set length to remove elements
console.log( ar ); // [1, 2, 3, 4]


var ar = [1, 2, 3, 4, 5, 6];

ar.pop(); // returns 6
console.log( ar ); // [1, 2, 3, 4, 5]

Method 3: Using Splice to Remove Array Elements in JavaScript

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

var removed = arr.splice(2,2);
console.log(arr);


var list = ["bar", "baz", "foo", "qux"];

list.splice(0, 2);
console.log(list);

Method 4: Removing Array Items By Value Using Splice

var arr = [1, 2, 6, 3, 2, 6, 7, 8, 9, 10];

for (var i = 0; i < arr.length; i++) {

if (arr[i] === 6) {

var removed = arr.splice(i, 1);
i--;
}

}

console.log(arr); // 6 is removed

Method 5: Using the Array filter Method to Remove Items By Value

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var filtered = array.filter(function(value, index, arr) {
return value != 6 ;
});

console.log(filtered); // 6 is removed

Method 6: The Lodash Array Remove Method

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var removeElement = _.remove(array, function(n) {
return n === 6;
});

console.log(array); // 6 is removed
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

Method 7: Making a Remove Method

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function arrayRemove(arr, value) {

return arr.filter(function(element) {
return element != value;
});
}

console.log(arrayRemove(array, 6)); // 6 is removed

Method 8: Explicitly Remove Array Elements Using the Delete Operator

var ar = [1, 2, 3, 4, 5, 6];

delete ar[4]; // delete element with index 4

console.log( ar ); // [1, 2, 3, 4, undefined, 6]

alert( ar ); // 1,2,3,4,,6

Method 9: Clear or Reset a JavaScript Array

var ar = [1, 2, 3, 4, 5, 6];
//do stuffar = [];
//a new, empty array!

var arr1 = [1, 2, 3, 4, 5, 6];
var arr2 = arr1;
// Reference arr1 by another variable arr1 = [];

console.log(arr2);
// Output [1, 2, 3, 4, 5, 6]

var arr1 = [1, 2, 3, 4, 5, 6];
var arr2 = arr1;
// Reference arr1 by another variable arr1 = [];

console.log(arr2);
// Output [1, 2, 3, 4, 5, 6]

Summary:

It is important to remove JavaScript Array items to manage your data. There is no single ‘remove’ method, but there are various methods and techniques you can use to clean up unwanted array items.

--

--