array diff in jquery

Solutions on MaxInterview for array diff in jquery by the best coders in the world

showing results for - "array diff in jquery"
Bear
18 Sep 2017
1/** SUBTRACT ARRAYS **/
2function subtractarrays(array1, array2){
3    var difference = [];
4    for( var i = 0; i < array1.length; i++ ) {
5        if( $.inArray( array1[i], array2 ) == -1 ) {
6                    difference.push(array1[i]);
7        }
8    }
9
10    return difference;
11}
12
13var I_like    = ["love", "sex", "food"];
14var she_likes = ["love", "food"];
15
16alert( "what I like and she does't like is: " + subtractarrays( I_like, she_likes ) ); //returns "Naughty"!