Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare 2 arrays which returns difference

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?

like image 235
Howdy_McGee Avatar asked Jun 07 '12 07:06

Howdy_McGee


People also ask

Can you compare two arrays?

In Java, we can compare two arrays by comparing each element of the array. Java Arrays class provides two predefined methods that is used to compare two arrays in Java. In this section, we will learn how to compare two Arrays using Arrays. equals() method and Arrays.


1 Answers

I know this is an old question, but I thought I would share this little trick.

var diff = $(old_array).not(new_array).get(); 

diff now contains what was in old_array that is not in new_array

like image 186
superphonic Avatar answered Oct 16 '22 10:10

superphonic