i have a three arrays in javascript
var array1 = new Array (1,2,3,4,5);
var array2 = new Array ("a", "b", "c", "d", "e");
var array3 = new Array ("a", "c", "d");
and i basically want to:
-
Create a new array with array2 minus items in array3. So it should result in
var array4 = new Array {“b”, “e”};
-
Create another array with the corresponding index of array1 that aligns with array 4, so in this case i would also want to be able to generate
var array5 = new Array {2, 5}
i know in dotnet 3.5 there are lots of simple methods to do this operation but wasn’t sure if javascript had anything similar.
Ok, for starters, array declaration takes one of two forms:
The second option is better, btw. See here for reasons.
What you’re actually looking for is something called difference (as opposed to intersection or union). There’s some other questions on Stack Overflow about array difference, like this one, however the accepted solution uses
for .. inon an array, which is not a good idea. I heard Doug Crockford literally punches you in the face through the Internet every time you do that. Here’s a more technical discussion about it if you’re interested.The answers to this question might suit you better.