Skip to content

Instantly share code, notes, and snippets.

@stanleycyang
Last active December 6, 2016 04:12
Show Gist options
  • Save stanleycyang/f15e70525cfc0cdacc593f3fb496f5e4 to your computer and use it in GitHub Desktop.
Save stanleycyang/f15e70525cfc0cdacc593f3fb496f5e4 to your computer and use it in GitHub Desktop.
function flatten(intArr, flatArr) {
var results = flatArr || [];
if (intArr && intArr.length > 0) {
intArr.forEach(function(value) {
if (typeof value === 'number') {
results.push(value);
} else if (value instanceof Array) {
flatten(value, results);
}
});
}
return results;
};
// Test cases
console.log(flatten([[1,2,[3]],4]))
console.log(flatten([[[1,2],[3]],4]))
console.log(flatten([[1,[2,[3]]],4]))
console.log(flatten([[1,2,[3]],[4,8,9]]))
console.log(flatten([[1,2,[3]],[4, [4,6]]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment