What's the fastest/best way to compare two arrays and return the difference? Much like array_diff
in PHP. Is there an easy function or am I going to have to create one via each()
? or a foreach
loop?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
This should work with unsorted arrays, double values and different orders and length, while giving you the filtered values form array1, array2, or both.
In this way you don't need to worry about if the first array is smaller than the second one.
if you also want to compare the order of the answer you can extend the answer to something like this:
I know this is an old question, but I thought I would share this little trick.
diff
now contains what was inold_array
that is not innew_array
Working demo http://jsfiddle.net/u9xES/
Good link (Jquery Documentation): http://docs.jquery.com/Main_Page {you can search or read APIs here}
Hope this will help you if you are looking to do it in JQuery.
The alert in the end prompts the array of uncommon element Array i.e. difference between 2 array.
Please lemme know if I missed anything, cheers!
Code