Remove item in array javascript by value
A one-liner will do it, Show
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 Basically,
filter is a convenience method that looks a lot nicer (and is chainable) as opposed to the
TuralAsgar 1,0772 gold badges11 silver badges24 bronze badges answered Dec 29, 2013 at 16:05
C BC B 11.4k4 gold badges35 silver badges47 bronze badges 8 This can be a global function or a method of a custom object, if you aren't allowed to add to native prototypes. It removes all of the items from the array that match any of the arguments.
To make it a global-
And to take care of IE8 and below-
answered Oct 17, 2010 at 20:16
kennebeckennebec 100k31 gold badges104 silver badges126 bronze badges 19 You can use underscore.js. It really makes things simple. For example, with this:
And In your case the code that you will have to write is:
It reduces the code that you write.
answered Feb 19, 2013 at 9:52
vatsalvatsal 3,7252 gold badges18 silver badges19 bronze badges 4 You can do it with these two ways:
answered Dec 26, 2017 at 15:40
AmerllicAAmerllicA 24.9k12 gold badges118 silver badges143 bronze badges 0 Check out this way:
and in a function:
answered Jul 17, 2011 at 17:31
gadlolgadlol 1,2432 gold badges14 silver badges23 bronze badges 3 The simplest solution is: array - array for remove some element valueForRemove; valueForRemove - element for remove;
More simple:
No pretty, but works:
No pretty, but works:
P.S. The filter() method creates a new array with all elements that pass the test implemented by the provided function. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter answered Feb 26, 2019 at 20:45
JackkobecJackkobec 5,11330 silver badges30 bronze badges 5 You can create your own method, passing throught the array and the value you want removed:
Then you can call this with:
answered May 10, 2020 at 18:03
ChiaroChiaro 1,36911 silver badges12 bronze badges Method 1
Method 2One liner Solution
answered Oct 29, 2018 at 16:15
Dev MateeDev Matee 4,5162 gold badges28 silver badges31 bronze badges 2 Here's a version that uses jQuery's inArray function:
answered Jul 12, 2013 at 9:02
CorayThanCorayThan 16.4k27 gold badges109 silver badges154 bronze badges 2
answered Nov 13, 2012 at 13:49
KldKld 6,7853 gold badges35 silver badges49 bronze badges ES6 way.
answered May 29, 2017 at 14:34
Oliver DixonOliver Dixon 6,6135 gold badges55 silver badges87 bronze badges 4 If you have unique values and ordering doesn't matter, use Set, it has delete():
answered May 18, 2017 at 14:05
greenegreene 5995 silver badges6 bronze badges Really, i can't see why this can't be solved with
Or maybe you want to use vanilla JS
answered Jul 13, 2018 at 1:18
rbenvenutorbenvenuto 2,4152 gold badges11 silver badges19 bronze badges 3 When you need to remove a value present multiple times in the array(e.g. [1,2,2,2, 4, 5,6]).
You can use splice to remove a single element from the array but splice can't remove multiple similar elements from the array.
Olcay Ertaş 5,7658 gold badges77 silver badges105 bronze badges answered Feb 13, 2018 at 7:27
2
Seeing as there isn't a pretty one, here's a simple and reusable ES6 function.
Usage:
answered Jun 26, 2017 at 11:44
ShakespeareShakespeare 1,2869 silver badges15 bronze badges 3 Removing all matching elements from the array (rather than just the first as seems to be the most common answer here):
I used jQuery for the heavy lifting, but you get the idea if you want to go native. answered Jan 22, 2014 at 17:04
JasonJason 2,1912 gold badges22 silver badges22 bronze badges 1 In all values unique, you can:
answered Nov 23, 2018 at 10:58
a very clean solution working in all browsers and without any framework is to asign a new Array and simply return it without the item you want to delete:
dcordz 1302 silver badges8 bronze badges answered Jan 7, 2016 at 13:52
mtizzianimtizziani 83810 silver badges22 bronze badges 1
I haven't got an IE6 around to check, but I'd call it a safe bet that you can check at least a million array items per second this way on almost any client machine. If [array size]*[searches per second] may grow bigger than a million you should consider a different implementation. Basically you can use an object to make an index for your array, like so:
Any sane JavaScript environment will create a searchable index for such objects so that you can quickly translate a key into a value, no matter how many properties the object has. This is just the basic method, depending on your need you may combine several objects and/or arrays to make the same data quickly searchable for different properties. If you specify your exact needs I can suggest a more specific data structure. answered Oct 17, 2010 at 18:32
aaaaaaaaaaaaaaaaaaaaaaaa 3,5781 gold badge23 silver badges23 bronze badges 1 You can achieve this using Lodash
answered Sep 3, 2018 at 9:11
Penny LiuPenny Liu 12.8k5 gold badges70 silver badges86 bronze badges You can use
answered Jan 12, 2016 at 22:39
sakoviassakovias 1,2961 gold badge17 silver badges25 bronze badges The trick is to go through the array from end to beginning, so you don't mess up the indices while removing elements.
arr is now ["red", "black", "white"] answered Mar 17, 2015 at 17:43
Non-destructive removal:
answered Jun 25, 2016 at 12:17
answered Jan 13, 2013 at 21:51
1 Please do not use the variant with
mmohab 2,2134 gold badges27 silver badges42 bronze badges answered May 5, 2014 at 16:10
Ilya SherIlya Sher 6015 silver badges4 bronze badges I used the most voted option and created a function that would clean one array of words using another array of unwanted words:
To use, do the following:
answered Feb 10, 2015 at 16:06
maudulusmaudulus 9,9599 gold badges73 silver badges110 bronze badges
answered Apr 18, 2018 at 9:02
Asif voraAsif vora 2,6723 gold badges12 silver badges28 bronze badges 0 In a global function we can't pass a custom value directly but there are many way as below
answered Jun 28, 2018 at 18:11
SrikrushnaSrikrushna 3,7651 gold badge36 silver badges45 bronze badges 1 How do I remove an item from an array by value?To remove an item from a given array by value, you need to get the index of that value by using the indexOf() function and then use the splice() function to remove the value from the array using its index.
How can I remove a specific item from an array?pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.
How do you delete a specific value from an array JavaScript?How to delete a value from an array in JavaScript. Use splice() to remove arbitrary item.. Use shift() to remove from beginning.. Use pop() to remove from end.. Using delete creates empty spots.. Remember this.. How do you remove an element from an array using splice?Find the index of the array element you want to remove using indexOf , and then remove that index with splice . The splice() method changes the contents of an array by removing existing elements and/or adding new elements. The second parameter of splice is the number of elements to remove.
|