Skip to content

Instantly share code, notes, and snippets.

@mirashif
Created June 10, 2021 14:40
Show Gist options
  • Save mirashif/675a25d940e9f2f1b091167a9d39ec1a to your computer and use it in GitHub Desktop.
Save mirashif/675a25d940e9f2f1b091167a9d39ec1a to your computer and use it in GitHub Desktop.
Here is a quick way to flatten a deeply nested array in Javascript.
const nested = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
const flatten = nested.flat(Infinity);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const result = flatten.join(", ");
// "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment