Created
November 21, 2016 15:31
-
-
Save misterussell/868a7b0c4960f6746541f73e9c743465 to your computer and use it in GitHub Desktop.
Function to return unique values from two arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function diff(arr1, arr2) { | |
// put the arrays together | |
// loop over the data | |
combinedArr = arr1.concat(arr2); | |
// filter over the combined array and compare the index and last index of each number | |
// if they match then return that number | |
let combedArray = combinedArr.filter((num) => { | |
if (combinedArr.indexOf(num) === combinedArr.lastIndexOf(num)) { | |
return num; | |
} | |
}); | |
return combedArray; | |
}; | |
arr1 = [1,6,4]; | |
arr2 = [3,1,2,4,9]; | |
console.log(diff(arr1, arr2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment