Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
dynamically populating variable with loop

I am dynamically generating json data as below

 <script>

        $(document).ready(function() {
        len = "<%- gsdata.length %>"


    for ( i = 0; i < len; i++) {
        //window.alert(len);
        var data = {

        "Results": [{
            "SCRIP": gsdata[i].SCRIP,
            "LTP": gsdata[i].LTP,
            "ALERT": gsdata[i].CAMARILLA,
            "TRIGERP": gsdata[i].ALERT,
            "BOOKAT": gsdata[i].BOOKAT,
            "PROFIT": gsdata[i].PROFIT,
        }]

        }
    }

        $('#scrip-data').DataTable({

        data: data.Results,
            columns: [

                {data: "SCRIP"},
                {data: "LTP"},
                {data: "ALERT"},
                {data: "TRIGERP"},
                {data: "BOOKAT"},
                {data: "PROFIT"},
            ],

        });
    } );
    </script>

This is an ejs template and loop is inside <script> tags. I am not able to get the value of i in the "SCRIP": "<%- gsdata[i].XXX %>". with "SCRIP": "<%- gsdata[1].SCRIP %>" I am able to get the values.

Any help here please

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I am guessing (i don't kow ejs), but i would think that you need to indicate that the for loop is part of the template, and not part of the script tag "output" :

<script>
$(document).ready(function() {
  len = "<%- gsdata.length %>"

   //window.alert(len);
    var data = {

      "Results": [ 
         <%- for (var i = 0; i < gsdata.length; i++) { %>
         {
          "SCRIP": "<%- gsdata[i].SCRIP %>",
          "LTP": "<%- gsdata[i].LTP %>",
          "ALERT": "<%- gsdata[i].CAMARILLA %>",
          "TRIGERP": "<%- gsdata[i].ALERT %>",
          "BOOKAT": "<%- gsdata[i].BOOKAT %>",
          "PROFIT": "<%- gsdata[i].PROFIT %>",
        },
        <%- } %>
    ]
  }
</script>

Updated to what I think poster is looking for... guessing though.

about 4 years ago · Juan Pablo Isaza Report

0

You don't need to use EJS syntax inside JavaScript.

And you shouldn't call JSON.stringify() to get the length of the array. That returns the length of the serialized array.

$(document).ready(function() {
  var len = gsdata.length;

  for (var i = 0; i < len; i++) {
    //window.alert(len);
    var data = {

      "Results": [{
        "SCRIP": gsdata[i].SCRIP,
        "LTP": gsdata[i].LTP,
        "ALERT": gsdata[i].CAMARILLA,
        "TRIGERP": gsdata[i].ALERT,
        "BOOKAT": gsdata[i].BOOKAT,
        "PROFIT": gsdata[i].PROFIT,
      }]
    }
  }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!