I have an array of integers, and I'm using the .push()
method to add elements to it.
Is there a simple way to remove a specific element from an array? The equivalent of something like array.remove(int);
.
I have to use core JavaScript - no frameworks are allowed.
Too old to reply, but may it help someone, by providing a predicate instead of a value.
NOTE: it will update the given array, and return affected rows
Usage
Definition
Based on all the answers which were mainly correct and taking into account the best practices suggested (especially not using Array.prototype directly), I came up with the below code:
Reviewing the above function, despite the fact that it works fine, I realised there could be some performance improvement. Also using ES6 instead of ES5 is a much better approach. To that end, this is the improved code:
How to use:
I am currently writing a blog post in which I have benchmarked several solutions for Array without problem and compared the time it takes to run. I will update this answer with the link once I finish that post. Just to let you know, I have compared the above against lodash's without and in case the browser supports
Map
, it beats lodash! Notice that I am not usingArray.prototype.indexOf
orArray.prototype.includes
as wrapping the exlcudeValues in aMap
orObject
makes querying faster!If you have complex objects in the array you can use filters? In situations where $.inArray or array.splice is not as easy to use. Especially if the objects are perhaps shallow in the array.
E.g. if you have an object with an Id field and you want the object removed from an array:
You have 1 to 9 array and you want remove 5 use below code.
If you want to multiple value ex :- 1,7,8
If you want to remove array value in array ex :- [3,4,5]
includes supported browser is link
Edited on 2016 october
In this code example I use "array.filter(...)" function to remove unwanted items from array, this function doesn't change the original array and creates a new one. If your browser don't support this function (e.g. IE before version 9, or Firefox before version 1.5), consider using the filter polyfill from Mozilla.
Removing item (ECMA-262 Edition 5 code aka oldstyle JS)
Removing item (ES2015 code)
IMPORTANT ES2015 "() => {}" arrow function syntax is not supported in IE at all, Chrome before 45 version, Firefox before 22 version, Safari before 10 version. To use ES2015 syntax in old browsers you can use BabelJS
Removing multiple items (ES2016 code)
An additional advantage of this method is that you can remove multiple items
IMPORTANT "array.includes(...)" function is not supported in IE at all, Chrome before 47 version, Firefox before 43 version, Safari before 9 version and Edge before 14 version so here is polyfill from Mozilla
Removing multiple items (Cutting-edge experimental JavaScript ES2018?)
Try it yourself in BabelJS :)
Reference
OK, for example you are having the array below:
And we want to delete number 4, you can simply do the below code:
If you reusing this function, you write a reusable function which will be attached to Native array function like below:
But how about if you are having the below array instead with few [5]s in the Array?
We need a loop to check them all, but easier and more efficient way is using built-in JavaScript functions, so we write a function which use filter like below instead:
Also there are third parties libraries which do help you to do this, like Lodash or Underscore, for more info look at lodash _.pull, _.pullAt or _.without.