138

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?

5
  • 7
    I think the question heading should be "JS - Remove an array element by value in JavaScript" Commented Jan 27, 2014 at 0:47
  • 2
    @kta It is! You used your mental-mind-powers and the heading reformed itself. Magic. Commented May 22, 2014 at 8:53
  • 1
    @User2 : I do believe in magic but in this case the Author had changed the question title after I wrote my first comment.:) Commented May 31, 2014 at 17:17
  • See also: Remove item from array by value and Remove specific element from an array? Commented Aug 11, 2014 at 16:49
  • find here: stackoverflow.com/a/71332975/14229690 Commented May 1, 2022 at 3:44

10 Answers 10

227

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.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 for the safety net.
I wouldn't inlcude jQuery just for that. A simple indexOf function is available in the MDN docs
@RobG: Absolutely right. I just assumed that since he tagged his question with jquery, he already has jQuery on the page
As noted in the comments to the linked explanation of using the tilde...don't use the tilde. It's unclear for basically no benefit.
tilde is not cool. Unmaintainable code is NOT cool!
19

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)

1 Comment

Not good to extend native JS objects. Plus, OP wants to return the new array with the element removed.. it would be nice to return the new array..
14
tag_story.splice(tag_story.indexOf(id_tag), 1);

11 Comments

Look at the question more carefully, it looks like he wants to remove a value from an array, not an index.
@Peter Removing an index removes the associated value.
This code is dangerous! If the value of id_tag is not found, it'll delete the last item in the array!! You'll have to first check if id_tag was found. See my answer.
@Matt : when stacks allows me :)) (5 minutes)
@Ispuk: This is a very bad habit. You should never use code simply because "it does what I need". You should carefully consider the consequences of every single line of code!!!
|
5

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;
    }
});

2 Comments

If you return x; in the callback then filter will also delete where Boolean(id_tag) = false. I think that's unintended.
Short way: id_tag = id_tag.filter(x => x !== 90);
4

As a variant

delete array[array.indexOf(item)];

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

3 Comments

Not a good solution, if you use delete keyword, you will have an undefined element in the array finally.
This was actually exactly what I needed. Together with array.forEach, I can easily loop through defined indecis.
Just a heads up that delete will blow up IE8 or lower...
4
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);

3 Comments

this doesn't works as i espected ... the Eli code was exactly what i needed but it doesn't works on IE, check that and check your code , they do not same work :( ...
this does not return the new array...
@AndreFigueiredo Thanks, I've edited to fix it.
3

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);
    }
};

1 Comment

OP wants to return the new array. he doesn't want to return the removed value..
2

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

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

Comments

1

You can use lodash.js

_.pull(arrayName,valueToBeRemove);

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

2 Comments

Please add some explenation to your answer and possibly the link to your source.
Hope this might help @Andre from [ lodash documnetation](lodash.com/docs#pull)
0
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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.