0

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 ?

11
  • What is ${} in your object? Commented May 6, 2016 at 15:25
  • expression language because im using it a jsp page Commented May 6, 2016 at 15:26
  • 2
    in your for loop, your never changing number so it'll run forever Commented May 6, 2016 at 15:28
  • Use i instead of number in for statement and remove i++ from end of it. Commented May 6, 2016 at 15:29
  • 1
    Also, this has nothing to do with JSON. Commented May 6, 2016 at 15:47

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

no that's what i want i want to get the i element ofDates table
0

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]]
    });
}

8 Comments

@Mehdi: "it doesn't work" doesn't contain any information to would enable us to help you. Please be more specific.
what exactly is ${obj} from anyways? Java? ES6?
Have a look at the comments on the question.
i use tax_data array for morris.js chart and i dont get any chart when i run this
Server-side only? Hell, the whole loop should be run server-side and return the compiled array to the JS code then
|
0

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

Comments

-1

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]]} }) ;

        }

2 Comments

What's the difference?
I see... that alone won't help though (likely just a typo here). There are much bigger issues with that code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.