Skip to content

Instantly share code, notes, and snippets.

@andreasonny83
Created September 21, 2019 10:10
Show Gist options
  • Save andreasonny83/17b23518aa1a0eb953e9a019bb32ab6f to your computer and use it in GitHub Desktop.
Save andreasonny83/17b23518aa1a0eb953e9a019bb32ab6f to your computer and use it in GitHub Desktop.
Flat an array recursively
const _ = require('lodash');
const x = [1, [2, 3, 4, [5, 6]], 7];
const flat = (input) => Array.isArray(input) ? _.flatMap(input, item => flat(item)) : input;
const y = flat(x)
y // [1, 2, 3, 4, 5, 6, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment