Skip to content

Instantly share code, notes, and snippets.

@lubien
Forked from Woodsphreaker/info.md
Last active March 30, 2017 03:57
Show Gist options
  • Save lubien/4b6bf2fc593a473eaba5f72290b9cd59 to your computer and use it in GitHub Desktop.
Save lubien/4b6bf2fc593a473eaba5f72290b9cd59 to your computer and use it in GitHub Desktop.

Recuperar todos os números de um array

Recupera todos arrays e retorna somente um com o valores.

No primeiro método há uma recursividade.

No segundo método, uma conversão para string, split e finalmente converte para um array final de números

Acho que pode ser útil também.

// https://repl.it/GlpS/0
const arr = [
[0, 1],
[2, 3],
[4, 5],
[6, 7, [8, 9, [10, 11, [12, 13], 14, 15, 16, [17, [18, [19, [20]]]]]]]
];
const flatten = xs =>
Array.isArray(xs)
? xs.reduce((acc, ys) => acc.concat(flatten(ys)), [])
: xs
flatten(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment