Assume we have the following arrays:
a = [1, 2, 3, 4, 5]
and
b = [2, 3]
How can I subtract b from a? So that we have c = a - b
which should be equal to [1, 4, 5]
. jQuery solution would also be fine.
Assume we have the following arrays:
and
How can I subtract b from a? So that we have |
|||
|
Assuming you're on a browser that has
If the browser in question does not have those methods, you may be able to shim them. |
|||||||||||
|
For code that would work in all browsers, you would have to manually find each element from b in a and remove it.
Working example here: http://jsfiddle.net/jfriend00/xkBzR/ And, here's a version that could be faster for large arrays because it puts everything into an object for hashed lookups rather than brute force array searching:
Working example here: http://jsfiddle.net/jfriend00/LUcx6/ |
|||||
|
Here an implementation for try works in all browsers:
|
|||
|