Skip to content

Instantly share code, notes, and snippets.

@ktrysmt
Last active October 5, 2016 07:13
Show Gist options
  • Save ktrysmt/06a6206726c8f4c04b77abbceba98ba8 to your computer and use it in GitHub Desktop.
Save ktrysmt/06a6206726c8f4c04b77abbceba98ba8 to your computer and use it in GitHub Desktop.
Flatten from a DOM Node included childNodes nested to a flat array.
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