Skip to content

Instantly share code, notes, and snippets.

@alexandrebvd
Created August 2, 2015 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandrebvd/065836f527a59527938a to your computer and use it in GitHub Desktop.
Save alexandrebvd/065836f527a59527938a to your computer and use it in GitHub Desktop.
20.Bonfire: Diff Two Arrays
function diff(arr1, arr2) {
function getDiff(arr1, arr2) {
var newArr = [];
for (var i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) === -1) {
newArr.push(arr2[i]);
}
}
return newArr;
}
var newArr = [];
newArr.push(getDiff(arr1, arr2));
newArr.push(getDiff(arr2, arr1));
console.log(newArr);
var flat = newArr.reduce(function(a, b) {
return a.concat(b);
});
return flat;
}
diff([1, 2, 3, 500], [1, 2, 3, 4, 5]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment