Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Created May 29, 2019 19:46
Show Gist options
  • Save aleclarson/e5228acf12f20ef1a5ac98e81f1e5040 to your computer and use it in GitHub Desktop.
Save aleclarson/e5228acf12f20ef1a5ac98e81f1e5040 to your computer and use it in GitHub Desktop.
// Search an array for nested arrays and inline them
const flatten = (arr, out = []) => {
arr.forEach(val => {
if (val == null) return
if (Array.isArray(val)) flatten(val, out)
else out.push(val)
})
return out
}
// Map an array and flatten it
const flatMap = (arr, mapper) => flatten(arr.map(mapper))
// Filter an array to remove the given values
const without = (arr, ...args) => arr.filter(val => !args.includes(val))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment