I'm trying to create an array of objects using:
var tax_data = new Array();
for (var i =0; i < ${Dates.size()}; i++) {
tax_data.push({
"period": "${Dates[i]}",
"Poids": ${WeightMesures[Dates[i]]},
"Nombre de pas": ${WalkingMesures[Dates[i]]},
"Pulsation": ${BpMesures[Dates[i]]}
});
}
but this doesn't work.
Is there an other method to put elements in this array ?
You seem to be mixing up iterating in JavaScript and iterating in your template engine. From ${Dates[i]}
it looks like you want i
to be a variable in your template, but you've declared it as a variable in your generated JavaScript.
Something like (untested)
var tax_data = new Array();
<c:forEach begin="1" end="Dates.size()" var="i">
tax_data.push({"period": "${Dates[i]}", "Poids": ${WeightMesures[Dates[i]]}, "Nombre de pas": ${WalkingMesures[Dates[i]]}, "Pulsation": ${BpMesures[Dates[i]]} }) ;
</c:forEach>
should result in a row for each date in the JavaScript
var tax_data = new Array();
tax_data.push({"period": "2014-01-01", "Poids": 32, ... }) ;
tax_data.push({"period": "2014-02-01", "Poids": 32, ... }) ;
tax_data.push({"period": "2014-02-01", "Poids": 32, ... }) ;
There isn't a simple way of having the generated JavaScript reference then Dates table without writing out each row of it, either directly or in response to a separate ajax request.
Pass the data to JavaScript variables first, then run your loop using the local vars:
var taxData = [],
index = 0,
dates = ${Dates},
weightMesures = ${WeightMesures},
walkingMesures = ${WalkingMesures},
bpMesures = ${BpMesures},
length = dates.length();
for (index; index < length}; index += 1) {
taxData.push({
"period": dates[i],
"Poids": weightMesures[dates[i]],
"Nombre de pas": walkingMesures[dates[i]],
"Pulsation": bpMesures[dates[i]]
});
}
I agree with Pete.
I advice you to create in vanilla a Json Array And writing again vanilla to retrieve properties in a loop.
Thus you would check that works in JavaScript.
It's bad pattern to mix JSP (or other templating language) with client-side JavaScript. You'd rather let a servlet to push Json Array towards a client page and then parse Json with pure JavaScript
Try this assuming you want to loop through Dates.size
var tax_data = [];
for (var i =0 ;i< ${Dates.size()}; i++) {
tax_data.push({"period": "${Dates[i]}", "Poids": ${WeightMesures[Dates[i]]}, "Nombre de pas": ${WalkingMesures[Dates[i]]}, "Pulsation": ${BpMesures[Dates[i]]} }) ;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.