Last active
November 12, 2017 15:13
-
-
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
This file contains hidden or 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
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