Search code examples
javascriptarraysjspforeachjstl

How to initialize array (in Javascript) with c:forEach tag?


I'm a bit confused. I'd like to know if there's a way to initialize an Array in javascript inside a c:forEach tag.

var Array = [];
   <c:forEach var="item" items="${data}">
       // something like -> Array[i] = ${item.user.fullName}
   </c:forEach>

${data} is passed to the .tag page through this command at the beginning:

<jsp:directive.attribute name="data" required="true" rtexprvalue="true" type="java.util.Collection" />

I know that something is working cause I see few fullName sometimes.

I don't know exactly if this is possible. I'm on this project and I'm trying to solve this.

Thanks.


Solution

  • I solved it! This is the code:

    var Array = [];
          <c:forEach var="item" items="${data}" varStatus="yourStatus">
            Array[${yourStatus.index}] = '${item.user.fullName}';
          </c:forEach>
    

    I found a perfect solution for this case. Thanks.