Skip to content

Instantly share code, notes, and snippets.

@scottlepp
Created November 26, 2018 15:17
Show Gist options
  • Save scottlepp/32ea0585c47ffbfbcd128f7fcc15e5ed to your computer and use it in GitHub Desktop.
Save scottlepp/32ea0585c47ffbfbcd128f7fcc15e5ed to your computer and use it in GitHub Desktop.
function flatten(data) {
return data.reduce(( acc, current ) => {
if (Array.isArray(current)) {
return acc.concat(flatten(current));
} else {
return acc.concat(current);
}
}, [])
}
const test = [[1,2,[3]],4];
const actual = flatten(test);
// Test Assertions
// Flattened length should equal 4
if (actual.length !== 4) {
console.error('Failed: Length Not Eq 4');
}
// Flattened values should not contain array
for (const val of actual) {
if (Array.isArray(val)) {
console.error('Failed: Value is Array');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment