Finding and removing a Javascript array?

I have:

var array = new Array(); array.push("A"); array.push("B"); array.push("C"); 

I want to be able to do something like:

array.remove("B");

but the delete function is missing. How to do it?

+104
javascript arrays
Mar 20 '12 at 18:40
source share
11 answers

In fact, I am updating this thread with a more recent single-line solution:

 let arr = ['A', 'B', 'C']; arr = arr.filter(e => e !== 'B'); // will return ['A', 'C'] 

The basic idea is to filter the array by selecting all elements other than the element you want to delete.

Note: will delete all occurrences.

+113
Jun 08 '17 at 10:21 on
source share

Scroll the list in reverse order and use the .splice method.

 var array = ['A', 'B', 'C']; // Test var search_term = 'B'; for (var i=array.length-1; i>=0; i--) { if (array[i] === search_term) { array.splice(i, 1); // break; //<-- Uncomment if only the first term has to be removed } } 

The reverse order is important when all occurrences of a search query need to be deleted. Otherwise, the counter will increase, and you will skip the items.

When you need to remove only the first occurrence, the following will also work:

 var index = array.indexOf(search_term); // <-- Not supported in <IE9 if (index !== -1) { array.splice(index, 1); } 
+169
Mar 20 '12 at 18:41
source share

Demo

You need to find the location of what you are looking for .indexOf() , then delete it with .splice()

 function remove(arr, what) { var found = arr.indexOf(what); while (found !== -1) { arr.splice(found, 1); found = arr.indexOf(what); } } var array = new Array(); array.push("A"); array.push("B"); array.push("C");​ remove(array, 'B'); alert(array)​​​​; 

This will take care of all meetings.

+19
Mar 20 '12 at 19:16
source share

Single liner list

Let me solve this problem for this array:

 var array = ['A', 'B', 'C']; 

1. Remove only the first: use if you are sure that the element exists, is not supported in <IE9

 array.splice(array.indexOf('B'), 1); 

2. Remove only the latter: use if you are sure that the element exists, is not supported in <IE9

 array.splice(array.lastIndexOf('B'), 1); 

3. Delete all occurrences: not supported in <IE9

 array = array.filter(v => v !== 'B'); 
+18
May 26 '18 at 21:45
source share

Simply

 array.splice(array.indexOf(item), 1); 
+13
Jul 27 '18 at 18:36
source share

You can solve this problem by adding a delete function to the prototype array.

 Array.prototype.remove = function(elem, all) { for (var i=this.length-1; i>=0; i--) { if (this[i] === elem) { this.splice(i, 1); if(!all) break; } } return this; }; 

Using:

 var myArray = ['A', 'B', 'C', 'D', 'A'] myArray.remove('A'); => ["A", "B", "C", "D"] myArray.remove('A', true); => ["B", "C", "D"] 

But be careful, expanding your own prototypes, there may be an anti-pattern!

+2
Apr 10 '13 at 9:12
source share

Simple Solution (ES6)

If you do not have a duplicate element

 Array.prototype.remove = function(elem) { var indexElement = this.findIndex(el => el === elem); if (indexElement != -1) this.splice(indexElement, 1); return this; }; 

Online demo (violin)

+2
Dec 02 '17 at 8:51
source share

use:

 array.splice(2, 1); 

This removes one element from the array, starting at index 2 (third element)

+1
Mar 20 2018-12-18T00:
source share

You must write your own delete. You can iterate over the array, grab the index of the element you want to delete, and use splice to delete it.

Alternatively, you can create a new array, create a loop over the current array, and if the current object does not match what you want to delete, put it in a new array.

+1
Mar 20 2018-12-18T00:
source share

use array.splice

 /*array.splice(index , howMany[, element1[, ...[, elementN]]]) array.splice(index) // SpiderMonkey/Firefox extension*/ array.splice(1,1) 

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

0
Mar 20 '12 at 18:44
source share
 const changedArray = array.filter( function(value) { return value !== 'B' }); 

or you can use:

 const changedArray = array.filter( (value) => value === 'B'); 

The modified array will contain no value of 'B'

0
Jun 12 '19 at 13:52 on
source share



All Articles