Skip to content

Instantly share code, notes, and snippets.

@chriswrightdesign
Last active June 23, 2017 03:40
Show Gist options
  • Save chriswrightdesign/9429b5c2b872c9ffa78018d82cff9b3b to your computer and use it in GitHub Desktop.
Save chriswrightdesign/9429b5c2b872c9ffa78018d82cff9b3b to your computer and use it in GitHub Desktop.
DOM reducing
const renderSample = text => {
const div = document.createElement('div');
div.className = 'my-shiny-div';
const textNode = document.createTextNode(text);
div.appendChild(textNode);
return div;
}
const titles = ['hello', 'yep', 'nope', 'maybe', 'goodbye'];
const titleDiv = titles.reduce((accum, current) => {
accum.appendChild(renderSample(current));
return accum;
}, document.createElement('div'));
/* result:
<div>
<div class="my-shiny-div">hello</div>
<div class="my-shiny-div">yep</div>
<div class="my-shiny-div">nope</div>
<div class="my-shiny-div">maybe</div>
<div class="my-shiny-div">goodbye</div>
</div>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment