0

I'm having a HTML page with JSP in it and returned a List from JSP.

<input type="hidden" id="role" value="${sessionScope.rolePermissionsList}" />

Using JavaScript, i tried to display the List:

var rolesList = document.getElementById('role');
console.log(role.value);

It displayed the below result on the browser's console:

[ PR_1 , PR_2, PR_3, PR_4 ]

I want this output to be converted to an Array of Strings.

I expected the output to be in this way [ "PR_1" , "PR_2", "PR_3", "PR_4" ]

Can anyone tell me how to achieve that?

1 Answer 1

1

Assuming [ PR_1 , PR_2, PR_3, PR_4 ] is a string, you can use the below to convert into an array of strings.

var rolesList = "[PR_1, PR_2, PR_3, PR_4]";
console.log(rolesList.match(/\[(.*?)\]/)[1].split(", "));

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

1 Comment

Worked like charm. Perfect !

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.