-1

my .js file has array json data as below

array1 =[{"id":"100","name":"abc","group":"123"} {"id":"102","name":"def","group":"456"} {"id":"103","name":"ghi","group":"789"}]

and i send data to html as below

app.post('/lms',function(req,res){
res.render('indextest',{data:array1});});

now i am able to access the above data in my indextest.html as below

<%= data[0].id%>

but my requirment is i want to access this data in a for loop

for(i=0;i<len;i++){
var id= <%= data[i].id%>;
document.write("<input type=\"checkbox\" name=\"bedID\" value=\""+id+"\">");}

here i am getting "i" is undefined and i pass as below

var id= '<%= data['+i+'].id%>';

i get id not defined

can some one help me in any other way to acces the data in the loop or fix the above issue thanks

sai kiran
  • 37
  • 1
  • 8

1 Answers1

1

See What is the difference between client-side and server-side programming? .

A variable in your Node-side JS is not accessible to a variable in your browser-side JS and vice versa.

You could convert the array to a JSON text and then write JavaScript into the page which treats that JSON text as a JavaScript array literal. Then you can process it entirely client side.

A better option, since your browser-side JS does nothing except generate HTML as a one time deal, would be to generate the HTML with Node instead of with browser-side JS.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335