Javascript array search and remove string?

JavascriptArrays

Javascript Problem Overview


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 there is no remove function. How do I accomplish this?

Javascript Solutions


Solution 1 - Javascript

I'm actually updating this thread with a more recent 1-line solution:

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

The idea is basically to filter the array by selecting all elements different to the element you want to remove.

Note: will remove all occurrences.

EDIT:

If you want to remove only the first occurence:

t = ['A', 'B', 'C', 'B'];
t.splice(t.indexOf('B'), 1); // will return ['B'] and t is now equal to ['A', 'C', 'B']

Solution 2 - Javascript

Loop through 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 the search term has to be removed. Otherwise, the counter will increase, and you will skip elements.

When only the first occurrence has to be removed, the following will also work:

var index = array.indexOf(search_term);    // <-- Not supported in <IE9
if (index !== -1) {
    array.splice(index, 1);
}

Solution 3 - Javascript

List of One Liners

Let's solve this problem for this array:

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

1. Remove only the first: Use If you are sure that the item exist

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

2. Remove only the last: Use If you are sure that the item exist

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

3. Remove all occurrences:

array = array.filter(v => v !== 'B'); 

Solution 4 - Javascript

DEMO

You need to find the location of what you're looking for with .indexOf() then remove 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 occurrences.

Solution 5 - Javascript

Simply

array.splice(array.indexOf(item), 1);

Solution 6 - Javascript

Simple solution (ES6)

If you don't have 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 (fiddle)

Solution 7 - Javascript

const changedArray = array.filter( function(value) {
  return value !== 'B'
});

or you can use :

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

The changedArray will contain the without value 'B'

Solution 8 - Javascript

You have to write you own remove. You can loop over the array, grab the index of the item you want to remove, and use splice to remove it.

Alternatively, you can create a new array, loop over the current array, and if the current object doesn't match what you want to remove, put it in a new array.

Solution 9 - Javascript

use:

array.splice(2, 1);

This removes one item from the array, starting at index 2 (3rd item)

Solution 10 - Javascript

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

Solution 11 - Javascript

This only valid on str list, look up this

myStrList.filter(item=> !["deletedValue","deletedValue2"].includes(item))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRolandoView Question on Stackoverflow
Solution 1 - JavascriptTyrannasView Answer on Stackoverflow
Solution 2 - JavascriptRob WView Answer on Stackoverflow
Solution 3 - JavascriptenesnView Answer on Stackoverflow
Solution 4 - JavascriptqwertymkView Answer on Stackoverflow
Solution 5 - JavascriptMattView Answer on Stackoverflow
Solution 6 - JavascriptAli SoltaniView Answer on Stackoverflow
Solution 7 - Javascriptsiva gopiView Answer on Stackoverflow
Solution 8 - JavascripthvgotcodesView Answer on Stackoverflow
Solution 9 - JavascriptBen ClaytonView Answer on Stackoverflow
Solution 10 - Javascriptchepe263View Answer on Stackoverflow
Solution 11 - JavascriptdpmemcryView Answer on Stackoverflow