Last active
October 5, 2016 07:13
-
-
Save ktrysmt/06a6206726c8f4c04b77abbceba98ba8 to your computer and use it in GitHub Desktop.
Flatten from a DOM Node included childNodes nested to a flat array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var r = [] | |
const flatten = (a) => { | |
if (a.firstChild === null) { | |
r.push(a) | |
} | |
else if (a.childNodes.length > 0) { | |
var c = a.childNodes | |
Array.from(c).forEach(function (v) { | |
if (v.firstChild === null) { | |
r.push(v) | |
} | |
else { | |
flatten.apply(this, [v]) | |
} | |
}) | |
} | |
return r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment