Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

90
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda