Reputation: 10254
Hello all I have a bean that has 3 getters. In the JSP I use JSTL to iterate over the bean to populate a table. I have saome javascript I need to do the same thing to construct an array. Here it is hardcoded, but how can I contruct it by itearing over a bean?
Bean: This is how I do it in the JSP using JSTL
<c:forEach var="bean" items="${beans}">
${bean.month}
</c:forEach>
How Can I do the same thing here:
Javascript:
"categories": [{
"category": [{
"label": "Oct"
}, {
"label": "Nov"
}, {
"label": "Dec"
}, {
"label": "Jan"
}, {
"label": "Feb"
}, {
"label": "Mar"
}, {
"label": "Apr"
}, {
"label": "May"
}, {
"label": "Jun"
}, {
"label": "Jul"
}, {
"label": "Aug"
}, {
"label": "Sep"
}]
}]
Trying to do something like this in javascript
<c:forEach var="bean" items="${beans}">
[{
"label": " ${bean.month}"
},
</c:forEach>
Upvotes: 0
Views: 1219
Reputation: 2036
var category = [], // new Array
i,
newCategory;
for (i = 0; i < beans.length; i += 1) {
newCategory = {}; // new object
newCategory.label = beans[i].month;
category.push(newCategory);
}
Upvotes: 0
Reputation: 25145
I am not well experienced in JSTL. This is a guess based on the experience I have in PHP.
var array = [
<c:forEach var="bean" items="${beans}" varStatus="beanStatus">
{
"label": "${bean.month}"
}
<c:if test="${!beanStatus.last}"> // put comma after all item, but last one
,
</c:if>
</c:forEach>
];
or
var array = [];
<c:forEach var="bean" items="${beans}">
array.push({
"label": "${bean.month}"
});
</c:forEach>
Upvotes: 1