137

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?

itsme
  • 48,972
  • 96
  • 224
  • 345
  • 7
    I think the question heading should be "JS - Remove an array element by value in JavaScript" – kta Jan 27 '14 at 00:47
  • 2
    @kta It is! You used your mental-mind-powers and the heading reformed itself. Magic. – User2 May 22 '14 at 08: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.:) – kta May 31 '14 at 17:17
  • See also: [Remove item from array by value](http://stackoverflow.com/q/3954438/1048572) and [Remove specific element from an array?](http://stackoverflow.com/q/7142890/1048572) – Bergi Aug 11 '14 at 16:49
  • find here: https://stackoverflow.com/a/71332975/14229690 – Sahil Thummar May 01 '22 at 03:44

10 Answers10

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.

Community
  • 1
  • 1
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • 15
    +1 for the safety net. – alex Aug 22 '11 at 03:36
  • 1
    I wouldn't inlcude jQuery just for that. A simple indexOf function is available in the [MDN docs](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf) – RobG Aug 22 '11 at 03:57
  • @RobG: Absolutely right. I just assumed that since he tagged his question with [tag:jquery], he already has jQuery on the page – Joseph Silber Aug 22 '11 at 14:19
  • 3
    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. – Dustin Wyatt May 12 '14 at 20:22
  • 1
    tilde is not cool. Unmaintainable code is NOT cool! – jperelli Mar 27 '15 at 23:20
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)
Pete
  • 2,196
  • 1
  • 17
  • 25
  • 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.. – Andre Figueiredo Nov 19 '14 at 21:59
14
tag_story.splice(tag_story.indexOf(id_tag), 1);
Eli Grey
  • 35,104
  • 14
  • 75
  • 93
  • 1
    Look at the question more carefully, it looks like he wants to remove a value from an array, not an index. – Peter Olson Aug 22 '11 at 03:31
  • 1
    @Peter Removing an index removes the associated value. – alex Aug 22 '11 at 03:32
  • 3
    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. – Joseph Silber Aug 22 '11 at 03:35
  • @Matt : when stacks allows me :)) (5 minutes) – itsme Aug 22 '11 at 03:37
  • @joseph Silber ... don't know if dangerous but it does what i need!! :) .... maybe my question was not so clear,i'm sorry if it was! – itsme Aug 22 '11 at 03:39
  • Just remember that Array.indexOf doesn't exist by default in IE6, IE7 or IE8 so this code won't work in < IE9 without either changing it or installing a shim to add Array.prototype.indexOf – jfriend00 Aug 22 '11 at 03:39
  • uhm ... i didn't checked on IE :/ ... thanks for alert!! i need to go .. later i will check better answers and test , then accept right answer ;) – itsme Aug 22 '11 at 03:41
  • 2
    @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!!! – Joseph Silber Aug 22 '11 at 03:41
  • @Joseph i'm running and i didn't checked on all browser cause of my 500mb ram pc, it still loading and loading ... i do not have bad habits, but bad pc :// – itsme Aug 22 '11 at 03:44
  • I am under the pretense that Ispuk knows that id_tag is in his array. If not, he should make sure the index is not -1 before splicing. – Eli Grey Aug 22 '11 at 03:52
  • i just tested your code Eli .. on IE8 it doesn't works unfortunately :( ... it was really good on FF :( – itsme Aug 22 '11 at 13:22
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;
    }
});
Aleck Landgraf
  • 1,545
  • 14
  • 13
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);
Peter Olson
  • 139,199
  • 49
  • 202
  • 242
4

As a variant

delete array[array.indexOf(item)];

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

disfated
  • 10,633
  • 12
  • 39
  • 50
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);
    }
};
yckart
  • 32,460
  • 9
  • 122
  • 129
2

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

tag_story.splice(tag_story.indexOf(90),1);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
J. Holmes
  • 18,466
  • 5
  • 47
  • 52
1

You can use lodash.js

_.pull(arrayName,valueToBeRemove);

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

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); 
Siddhartha
  • 1,473
  • 14
  • 10