Created
November 2, 2017 16:02
-
-
Save nerboda/a814d36b8e5c049e4a18708ce70dd2a6 to your computer and use it in GitHub Desktop.
Flatten And Unique
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 isStringNumber(input) { | |
return !!(typeof input === 'string' && input.match(/^\d+$/)); | |
} | |
function includes(array, search) { | |
return array.some(function(element) { | |
if (typeof search === 'number' || isStringNumber(search)) { | |
return search == element; | |
} else { | |
return search === element; | |
} | |
}) | |
} | |
function flattenAndUnique(array) { | |
var output = []; | |
array.forEach(function(subarray) { | |
subarray.forEach(function(element) { | |
if (!includes(output, element)) { | |
output.push(element); | |
} | |
}) | |
}) | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment