javascript - Compare 2 arrays which returns difference -
what's fastest/best way compare 2 arrays , return difference? array_diff
in php. there easy function or going have create 1 via each()
? or foreach
loop?
working demo http://jsfiddle.net/u9xes/
good link (jquery documentation): http://docs.jquery.com/main_page {you can search or read apis here}
hope if looking in jquery.
the alert in end prompts array of uncommon element array i.e. difference between 2 array.
please lemme know if missed anything, cheers!
code
var array1 = [1, 2, 3, 4, 5, 6]; var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var difference = []; jquery.grep(array2, function(el) { if (jquery.inarray(el, array1) == -1) difference.push(el); }); alert(" difference " + difference); // changed variable name
Comments
Post a Comment