Is there a method to remove an item from a JavaScript array?
Given an array:
var ary = ['three', 'seven', 'eleven'];
I would like to do something like:
removeItem('seven', ary);
I've looked into splice()
but that only removes by the position number, whereas I need something to remove an item by its value.
ES6 way.
CoffeeScript+jQuery variant:
it remove only one, not all.
A one-liner will do it,
This makes use of the filter function in JS. It's supported in IE9 and up.
What it does (from the doc link)
So basically, this is the same as all the other
for (var key in ary) { ... }
solutions, except that thefor in
construct is supported as of IE6.Basically, filter is a convenience method that looks a lot nicer (and is chainable) as opposed to the
for in
construct (AFAIK).What you're after is filter
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
This will allow you to do the following:
This was also noted in this thread somewhere else: https://stackoverflow.com/a/20827100/293492
You can do it with these two ways:
first:
second (ES6):