2

I am trying to create an array object based on data that came from the servlet since trying to c:if with a Javascript variable was invalid. The session variable 'preeditList' is an array of 3 fields. Here is the following array in my JSP:

  var checkedArray = 
      [
        <c:forEach items="${preeditList}" var="preeditList" varStatus="status">  
           {"schedulekey": '${preeditList.getCHK_SCHEDULE_NUMBER()}',
            "contractkey": '${preeditList.getCHK_CONTRACT_YEAR()}',
            "prevStatus": '${preeditList.getCHK_STATUS()}' 
           }
           <c:if test="${!status.last}">    
           ,    
          </c:if>  
       </c:forEach>  
     ]  ;

This is giving me lots of syntax errors saying that a comma is expected. First of all should the array come out looking like this or completely screwed up:

 var checkArray = [
    {
     "schedulekey": "43080",
     "contractkey": "2016",
     "prevStatus": "RP"
     },
    {
     "schedulekey": "43070",
     "contractkey": "2016",
     "prevStatus": "CP"
     } 
    ]  ;                            

If this is fine then what I wrong on my syntax to pull this off.

Thanks again

1 Answer 1

4
var countries = [
<c:forEach items="${countryList}" var="country" varStatus="status">  
    {country: '${country.name}',
    provinces : [ 
        <c:forEach items="${country.provinces}" var="province" varStatus="provinceStatus">  
           '${province.name}'
           <c:if test="${!provinceStatus.last}">    
             ,    
           </c:if>   
        </c:forEach>  
    ]}
    <c:if test="${!status.last}">    
      ,    
    </c:if>  
    </c:forEach>  
];

Other Way :

var countries = new Array();
<c:forEach items="${countryList}" var="country" varStatus="status"> 
    countryDetails = new Object();
    countryDetails.country = ${country.name}; 
    var provinces = new Array();

        <c:forEach items="${country.provinces}" var="province" varStatus="provinceStatus"> 
           provinces.push(${province.name});
        </c:forEach> 
    countryDetails.provinces = provinces;
    countries.push(countryDetails);
</c:forEach> 

OutPut:

var countries = [
  {country:"USA",
  provinces: [
    "Ohio",
    "New York",
    "California"
  ]},
  {country:"Canada",
  provinces: [
    "Ontario",
    "Northern Territory",
    "Sascetchewan"
  ]},
]

I found solution from Problems ..... JSTL To JS

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

Comments

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.