-
-
Save long25vn/e6e07ed23e2fe4c475d201bb5a9a02f2 to your computer and use it in GitHub Desktop.
Template Literals example: For loops
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
/* | |
Template literals for-loop example | |
Using `Array(5).join(0).split(0)`, we create an empty array | |
with 5 items which we can iterate through using `.map()` | |
*/ | |
var element = document.createElement('div') | |
element.innerHTML = ` | |
<h1>This element is looping</h1> | |
${Array(5).join(0).split(0).map((item, i) => ` | |
<div>I am item number ${i}.</div> | |
`).join('')} | |
` | |
/* | |
Results: | |
<div> | |
<h1>This element is looping</h1> | |
<div>I am item number 0.</div> | |
<div>I am item number 1.</div> | |
<div>I am item number 2.</div> | |
<div>I am item number 3.</div> | |
<div>I am item number 4.</div> | |
</div> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment