how do I use for loop inside script tag in ejs












0















I try to draw chart, using chart.js.



To get my data, I try to use ejs tags



for example,
in ejs I input html like this, and it works well.



      <p>date: <%= today %></p>
<p>temperature: <%= data[data.length-1].temperature %>℃</p>

<h1>5days average temperature</h1>

<ul>
<% for(var i = 0; i < 5; i++) {(function (j) {%>
<li><%= dateList[j] %>%></li>
<ul><li><%= avgLocTmpList[i] %></li></ul>
<% })(i);} %>
</ul>


but when I use chart.js, I have to use ejs tags like this inside .



<script>
var temper0 = '<%= data[0].temperature%>';
tempList.push(temper0);
var temper1 = '<%= data[1].temperature%>';
tempList.push(temper1);
var temper2 = '<%= data[2].temperature%>';
tempList.push(temper2);
</script>


I can get data[0].temperature as well inside tag.



but, I hope to use for loop inside .
such as



<% for(var i = 0; i < 5; i++) {
<%= data[i].temperature %>
<% }%>


but I can't handle this.
Is there any option or way to handle this for loop inside ??



Thanks, in advance.










share|improve this question



























    0















    I try to draw chart, using chart.js.



    To get my data, I try to use ejs tags



    for example,
    in ejs I input html like this, and it works well.



          <p>date: <%= today %></p>
    <p>temperature: <%= data[data.length-1].temperature %>℃</p>

    <h1>5days average temperature</h1>

    <ul>
    <% for(var i = 0; i < 5; i++) {(function (j) {%>
    <li><%= dateList[j] %>%></li>
    <ul><li><%= avgLocTmpList[i] %></li></ul>
    <% })(i);} %>
    </ul>


    but when I use chart.js, I have to use ejs tags like this inside .



    <script>
    var temper0 = '<%= data[0].temperature%>';
    tempList.push(temper0);
    var temper1 = '<%= data[1].temperature%>';
    tempList.push(temper1);
    var temper2 = '<%= data[2].temperature%>';
    tempList.push(temper2);
    </script>


    I can get data[0].temperature as well inside tag.



    but, I hope to use for loop inside .
    such as



    <% for(var i = 0; i < 5; i++) {
    <%= data[i].temperature %>
    <% }%>


    but I can't handle this.
    Is there any option or way to handle this for loop inside ??



    Thanks, in advance.










    share|improve this question

























      0












      0








      0








      I try to draw chart, using chart.js.



      To get my data, I try to use ejs tags



      for example,
      in ejs I input html like this, and it works well.



            <p>date: <%= today %></p>
      <p>temperature: <%= data[data.length-1].temperature %>℃</p>

      <h1>5days average temperature</h1>

      <ul>
      <% for(var i = 0; i < 5; i++) {(function (j) {%>
      <li><%= dateList[j] %>%></li>
      <ul><li><%= avgLocTmpList[i] %></li></ul>
      <% })(i);} %>
      </ul>


      but when I use chart.js, I have to use ejs tags like this inside .



      <script>
      var temper0 = '<%= data[0].temperature%>';
      tempList.push(temper0);
      var temper1 = '<%= data[1].temperature%>';
      tempList.push(temper1);
      var temper2 = '<%= data[2].temperature%>';
      tempList.push(temper2);
      </script>


      I can get data[0].temperature as well inside tag.



      but, I hope to use for loop inside .
      such as



      <% for(var i = 0; i < 5; i++) {
      <%= data[i].temperature %>
      <% }%>


      but I can't handle this.
      Is there any option or way to handle this for loop inside ??



      Thanks, in advance.










      share|improve this question














      I try to draw chart, using chart.js.



      To get my data, I try to use ejs tags



      for example,
      in ejs I input html like this, and it works well.



            <p>date: <%= today %></p>
      <p>temperature: <%= data[data.length-1].temperature %>℃</p>

      <h1>5days average temperature</h1>

      <ul>
      <% for(var i = 0; i < 5; i++) {(function (j) {%>
      <li><%= dateList[j] %>%></li>
      <ul><li><%= avgLocTmpList[i] %></li></ul>
      <% })(i);} %>
      </ul>


      but when I use chart.js, I have to use ejs tags like this inside .



      <script>
      var temper0 = '<%= data[0].temperature%>';
      tempList.push(temper0);
      var temper1 = '<%= data[1].temperature%>';
      tempList.push(temper1);
      var temper2 = '<%= data[2].temperature%>';
      tempList.push(temper2);
      </script>


      I can get data[0].temperature as well inside tag.



      but, I hope to use for loop inside .
      such as



      <% for(var i = 0; i < 5; i++) {
      <%= data[i].temperature %>
      <% }%>


      but I can't handle this.
      Is there any option or way to handle this for loop inside ??



      Thanks, in advance.







      javascript json chart.js ejs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 18 '18 at 9:43









      TH ChoTH Cho

      263




      263
























          1 Answer
          1






          active

          oldest

          votes


















          0














          In a similar situation i used this inside script tags and worked!
          So try the following and check if it works.



          <script type="text/javascript">
          <% data.forEach(function (d) { %>
          console.log("<%= d %>"); // or anything you want to do.
          <% }) %>
          </script>


          Also there is a mistake in your following loop code. Care about opening and closing ejs tags. In the first line you forgot to close the ejs tag.



          <% for(var i = 0; i < 5; i++) {
          <%= data[i].temperature %>
          <% }%>


          Use this: <% for(var i = 0; i < 5; i++) { %>






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52383660%2fhow-do-i-use-for-loop-inside-script-tag-in-ejs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            In a similar situation i used this inside script tags and worked!
            So try the following and check if it works.



            <script type="text/javascript">
            <% data.forEach(function (d) { %>
            console.log("<%= d %>"); // or anything you want to do.
            <% }) %>
            </script>


            Also there is a mistake in your following loop code. Care about opening and closing ejs tags. In the first line you forgot to close the ejs tag.



            <% for(var i = 0; i < 5; i++) {
            <%= data[i].temperature %>
            <% }%>


            Use this: <% for(var i = 0; i < 5; i++) { %>






            share|improve this answer






























              0














              In a similar situation i used this inside script tags and worked!
              So try the following and check if it works.



              <script type="text/javascript">
              <% data.forEach(function (d) { %>
              console.log("<%= d %>"); // or anything you want to do.
              <% }) %>
              </script>


              Also there is a mistake in your following loop code. Care about opening and closing ejs tags. In the first line you forgot to close the ejs tag.



              <% for(var i = 0; i < 5; i++) {
              <%= data[i].temperature %>
              <% }%>


              Use this: <% for(var i = 0; i < 5; i++) { %>






              share|improve this answer




























                0












                0








                0







                In a similar situation i used this inside script tags and worked!
                So try the following and check if it works.



                <script type="text/javascript">
                <% data.forEach(function (d) { %>
                console.log("<%= d %>"); // or anything you want to do.
                <% }) %>
                </script>


                Also there is a mistake in your following loop code. Care about opening and closing ejs tags. In the first line you forgot to close the ejs tag.



                <% for(var i = 0; i < 5; i++) {
                <%= data[i].temperature %>
                <% }%>


                Use this: <% for(var i = 0; i < 5; i++) { %>






                share|improve this answer















                In a similar situation i used this inside script tags and worked!
                So try the following and check if it works.



                <script type="text/javascript">
                <% data.forEach(function (d) { %>
                console.log("<%= d %>"); // or anything you want to do.
                <% }) %>
                </script>


                Also there is a mistake in your following loop code. Care about opening and closing ejs tags. In the first line you forgot to close the ejs tag.



                <% for(var i = 0; i < 5; i++) {
                <%= data[i].temperature %>
                <% }%>


                Use this: <% for(var i = 0; i < 5; i++) { %>







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 '18 at 21:51

























                answered Nov 9 '18 at 13:07









                dimitris tseggenesdimitris tseggenes

                485




                485






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52383660%2fhow-do-i-use-for-loop-inside-script-tag-in-ejs%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Full-time equivalent

                    Bicuculline

                    さくらももこ