Javascript - remove an array item by value

JavascriptArrays

Javascript Problem Overview


My situation:

var id_tag = [1,2,3,78,5,6,7,8,47,34,90];

I would like to delete where id_tag = 90 and to return:

var id_tag = [1,2,3,78,5,6,7,8,47,34];

How can I do that?

Javascript Solutions


Solution 1 - Javascript

You'll want to use JavaScript's Array splice method:

var tag_story = [1,3,56,6,8,90],
    id_tag = 90,
    position = tag_story.indexOf(id_tag);

if ( ~position ) tag_story.splice(position, 1);

P.S. For an explanation of that cool ~ tilde shortcut, see this post:

Using a ~ tilde with indexOf to check for the existence of an item in an array.


Note: IE < 9 does not support .indexOf() on arrays. If you want to make sure your code works in IE, you should use jQuery's $.inArray():

var tag_story = [1,3,56,6,8,90],
    id_tag = 90,
    position = $.inArray(id_tag, tag_story);

if ( ~position ) tag_story.splice(position, 1);

If you want to support IE < 9 but don't already have jQuery on the page, there's no need to use it just for $.inArray. You can use this polyfill instead.

Solution 2 - Javascript

If you're going to be using this often (and on multiple arrays), extend the Array object to create an unset function.

Array.prototype.unset = function(value) {
	if(this.indexOf(value) != -1) { // Make sure the value exists
		this.splice(this.indexOf(value), 1);
	}	
}

tag_story.unset(56)

Solution 3 - Javascript

tag_story.splice(tag_story.indexOf(id_tag), 1);

Solution 4 - Javascript

I like to use filter:

var id_tag = [1,2,3,78,5,6,7,8,47,34,90];

// delete where id_tag = 90
id_tag = id_tag.filter(function(x) {
    if (x !== 90) {
      return x;
    }
});

Solution 5 - Javascript

function removeValue(arr, value) {
    for(var i = 0; i < arr.length; i++) {
        if(arr[i] === value) {
            arr.splice(i, 1);
            break;
        }
    }
    return arr;
}

This can be called like so:

removeValue(tag_story, 90);

Solution 6 - Javascript

As a variant

delete array[array.indexOf(item)];

If you know nothing about delete operator, DON'T use this.

Solution 7 - Javascript

Here are some helper functions I use:

Array.contains = function (arr, key) {
    for (var i = arr.length; i--;) {
        if (arr[i] === key) return true;
    }
    return false;
};

Array.add = function (arr, key, value) {
    for (var i = arr.length; i--;) {
        if (arr[i] === key) return arr[key] = value;
    }
    this.push(key);
};

Array.remove = function (arr, key) {
    for (var i = arr.length; i--;) {
        if (arr[i] === key) return arr.splice(i, 1);
    }
};

Solution 8 - Javascript

You'll want to use .indexOf() and .splice(). Something like:

tag_story.splice(tag_story.indexOf(90),1);

Solution 9 - Javascript

You can use lodash.js

_.pull(arrayName,valueToBeRemove);

In your case :- _.pull(id_tag,90);

Solution 10 - Javascript

var id_tag = [1,2,3,78,5,6,7,8,47,34,90]; 
var delete_where_id_tag = 90
    id_tag =id_tag.filter((x)=> x!=delete_where_id_tag); 

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
QuestionitsmeView Question on Stackoverflow
Solution 1 - JavascriptJoseph SilberView Answer on Stackoverflow
Solution 2 - JavascriptPeteView Answer on Stackoverflow
Solution 3 - JavascriptEli GreyView Answer on Stackoverflow
Solution 4 - JavascriptAleck LandgrafView Answer on Stackoverflow
Solution 5 - JavascriptPeter OlsonView Answer on Stackoverflow
Solution 6 - JavascriptdisfatedView Answer on Stackoverflow
Solution 7 - JavascriptyckartView Answer on Stackoverflow
Solution 8 - JavascriptJ. HolmesView Answer on Stackoverflow
Solution 9 - JavascriptGourav SahareView Answer on Stackoverflow
Solution 10 - JavascriptSiddharthaView Answer on Stackoverflow