-4

I'm currently working with my web based system using nodejs and html.

Is there any alternative way to loop html elements without using forEach?

I'm thinking about like this(this is just an example):

 <% for(var ctr=0; ctr<arrayname.length;ctr++){ %>`
      `<input type="text">`
    `<%}%>`

I hope someone will answer my question. :)

moonwalker7
  • 1,122
  • 3
  • 11
  • 29
Sean Cortez
  • 127
  • 2
  • 2
  • 7

2 Answers2

1

There are many ways to iterate through 'iterables' in javascript as in any other language.

Please take a look to this link:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

Here you will find examples of all the possible ways:

  • for statement
  • do...while statement
  • while statement
  • labeled statement
  • break statement
  • continue statement
  • for...in statement
  • for...of statement

I hope it helps. If it doesn't fit you, it would be nice to know what and why you want to achieve.

Javier Cobos
  • 1,172
  • 10
  • 22
0

It seems that your issue is that you need to be access your iterating variable, which is workable through a .forEach call as the second argument in the function provided to the .forEach call.

Code below to show proof.

$(function(){
  var items = ['one','two','three'];
  items.forEach(function(item,ctr){
    $('#testing').append('<div class="ctr-'+ctr+'">'+item+' CTR: '+ctr+'</div>');
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="testing">
</div>
Jhecht
  • 4,407
  • 1
  • 26
  • 44