Skip to content

Instantly share code, notes, and snippets.

@aderaaij
Last active November 12, 2017 15:13
Show Gist options
  • Save aderaaij/c681dabec750feda8384809a84470cde to your computer and use it in GitHub Desktop.
Save aderaaij/c681dabec750feda8384809a84470cde to your computer and use it in GitHub Desktop.
A way to open a template string and loop through arrays or objects to create another template string within. From es6.io
const dogs = [
{ name: 'Snickers', age: 2 },
{ name: 'Hugo', age: 8 },
{ name: 'Sunny', age: 1 }
];
const markup = `
<ul class="dogs">
${dogs.map(dog => `
<li>
${dog.name}
is
${dog.age * 7}
</li>`).join('') // Add 'join' to remove the comma behind each item
}
</ul>
`;
document.body.innerHTML = markup;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment