When attempting to create a chart using chart.js, I encountered an issue.
In order to retrieve my data, I attempted to use ejs tags.
For example, within the ejs input HTML below, everything worked smoothly.
<p>date: <%= today %></p>
<p>temperature: <%= data[data.length-1].temperature %>℃</p>
<h1>5 days 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>
However, when working with chart.js, I had to use ejs tags in a different manner like demonstrated below.
<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 was able to successfully obtain data[0].temperature using this method.
However, I wished to incorporate a for loop within the script tag, like shown below:
<% for(var i = 0; i < 5; i++) {
<%= data[i].temperature %>
<% }%>
Unfortunately, I faced difficulties in implementing this for loop inside the script tag. Are there any suggestions or alternative methods for achieving this?
Thank you in advance.