Skip to content

Instantly share code, notes, and snippets.

@aethant
Last active December 29, 2016 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aethant/bcefb39aeed7c6f367678f4414e4e5fd to your computer and use it in GitHub Desktop.
Save aethant/bcefb39aeed7c6f367678f4414e4e5fd to your computer and use it in GitHub Desktop.
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4]. Your solution should be a link to a gist on gist.github.com with your implementation. When writing this code, you can use any language you're comfortable with. The code must be well tested and docum…
const a = [ 23, 3, [ 5, 12, ], 11, ]
const b = [ [23, [233, 9, ], 11, ], 13, ]
const c = [ [ [ 222, 34, 55, 1, 7, ], 9, 1500, ], ]
const shallowify = arr => {
let results = []
arr.forEach(arrEl => {
if (arrEl.constructor === Array) {
results = [...results, ...shallowify(arrEl), ]
} else {
results = [...results, arrEl, ]
}
})
return results
}
console.log(shallowify([a, b, c]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment