How to remove a specific value from a javascript array

Reading time: About 1 minute

I couldn’t find a short and sweet answer to this one, so I made an answer and I’m sharing it here if you need it. Enjoy!

// from this
var arr = ['remove', 'specific', 'value', 'from', 'javascript', 'array']; // 6 items
 
// to this ("specific" value removed)
var arr = ['remove', 'value', 'from', 'javascript', 'array']; //  5 items
 
// use this (removes "specific")
arr.splice(arr.indexOf('specific'), 1);
 
 
// full example
var arr = ['remove', 'specific', 'value', 'from', 'javascript', 'array'];
var value_to_remove = 'specific';
arr.splice(arr.indexOf(value_to_remove), 1);
 
 
// note: to support IE (anger rising as I type), you'll need this (thanks James!):
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;
 
    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;
 
    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

14 comments skip to comment form

  1. Christopher Scott said— 21 minutes later

    Dude; that’s awesome…i’m adding this to my code collection :)

    #1
  2. Brian Cray said— 27 minutes later

    Glad you liked it Christopher =)

    #2
  3. saurabh shah said— 51 minutes later

    cool ! nice t0 see this handy tip !

    #3
  4. Cameron said— 2 hours later

    Nice one Bri! very cool! :) Definitely going to save this one!

    #4
  5. Brian Cray said— 16 hours later

    Thanks guys!

    #5
  6. David said— 2 days later

    Keep in mind if you pass a value_to_remove that is NOT in the array, then you will remove the last one.

    #6
  7. anthony said— 2 days later

    This doesnt seem to work in IE. Arrays dont seem to have an indexOf function.

    #7
  8. Brian Cray said— 2 days later

    David: Very good point!

    Anthony: Interesting… which version of IE?

    #8
  9. James said— 3 days later

    The indexOf method is not supported everywhere, at least not for arrays. To fix this you can simply roll your own one, see: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference:Objects:Array:indexOf

    #9
  10. Brian Cray said— 4 days later

    James, thanks for the awesome link! Definitely good piece of code to have for backwards support! Really, thanks!

    #10
  11. lukemh said— 4 days later

    how is this new? all you did is use slice?

    #11
  12. lukemh said— 4 days later

    *splice

    #12
  13. Jayaprakash said— 4 days later

    Brian, it would be better if you update the original code with the changes pointed out by James. I mean show the code to integrate the indexOf method in Array object.

    As pointed out the original script will not work in IE environment without this modification.

    #13
  14. Brian Cray said— 5 days later

    Lukemh: Not everybody knows this combination will result in what it does. How many times have you search Google for a piece of code that did one particular thing?

    Jaya: You’re right. Updated.

    #14
  15. Respond to this post—

Return to navigation
1311