Skip to content

Instantly share code, notes, and snippets.

@dtmrc
Forked from aleclarson/es6.js
Created September 12, 2021 01:55
Show Gist options
  • Save dtmrc/418d8f06c6e08da38ca05e37dea55250 to your computer and use it in GitHub Desktop.
Save dtmrc/418d8f06c6e08da38ca05e37dea55250 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