/ Published in: JavaScript
                    
                                        
Compare two arrays and return a new array with any items only found in one of the original arrays.
ex.,
[1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return ["piglet", 4].
                ex.,
[1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return ["piglet", 4].
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
function diff(arr1, arr2) {
var newArr = [];
newArr = arr1.diff(arr2).concat(arr2.diff(arr1));
return newArr;
}
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};
function isSame(val1, val2) {
return val1 === val2;
}
diff([1, "calf", 3, "piglet"], [1, "calf", 3, 4] );
Comments
 Subscribe to comments
                    Subscribe to comments
                
                