0

Here is the jsp code embedded in Java Script:

//This object comes from Java code to Jsp Code.    
var msgs = [{name=rajasekhar, langId=en, bShow=true}, {name=sekhar, langId=de,bShow=false},{name=valli, langId=en, bShow=false}];


<script type="text/javascript">
var langSectState = new Array();

 <c:forEach var="msg" items="${msgs}">
    <c:set var="langId" value="${msg['langId']}"/>

    langSectState ["${langId}"] = ${msg['bShow']};  //JS

 </c:forEach>
</script>

MoreInfo: The above code It extracts langId, bShow from msgs and stores values in Array.

After my server configuration changes, This is not working fine. So, I have to change this code to full Java Script.

Can anyone suggest the equivalent code to this in Java Script

2
  • 1
    Why not use JSON encode? var langSectState=${msgs_json} where msgs_json is the json-encoded array of maps. Commented Dec 11, 2012 at 5:54
  • Hi @Jan Dvorak, Can u please explain it. How directly "msgs" can be converted into that form. How to encode msgs to msgs_json ? Commented Dec 17, 2012 at 12:03

1 Answer 1

1

I'm not sure if you really need to overwrite values in this array (for example, you have two objects with langId="en"). But your code looks like the following one.

<script type="text/javascript">
var msgs = [{name:"rajasekhar", langId:"en", bShow:true}, {name:"sekhar", langId:"de",bShow:false},{name:"valli", langId:"en", bShow:false}];
var langSectState = new Array();

for(var it in msgs)
{
    var langId = msgs[it]['langId'];
    langSectState[langId] = msgs[it]['bShow'];
}

for(var it in langSectState)
{
    console.log(it+":"+langSectState[it]);
}

</script>

Please note that console.log loop is added just for test purposes

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

1 Comment

Hi Mikali, Thanks for the code and Partially it solved my problem. The langId won't be duplicate. Its my mistake. But, the var msgs will have "=" sign instead of ":". Since it will be processed in jsp, I can't change the structure to ":" in Java 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.