Skip to content

Instantly share code, notes, and snippets.

@nerboda
Created November 2, 2017 16:02
Show Gist options
  • Save nerboda/a814d36b8e5c049e4a18708ce70dd2a6 to your computer and use it in GitHub Desktop.
Save nerboda/a814d36b8e5c049e4a18708ce70dd2a6 to your computer and use it in GitHub Desktop.
Flatten And Unique
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