Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity array to javascript array

I need to pass a velocity string array to JavaScript function. So, for that, I want to convert the velocity array into JavaScript array.

The source code looks like this :

String[] arrStr[] = new String[3];
arrStr[0] = "String 1";
arrStr[1] = "String 2";
arrStr[2] = "String 3";
request.setAttribute("a", arrStr);

In my HTML template,

#set ( #arr = $request.getAttribute("a"))

<script language="javascript">

var newArr = "${arr}";

</script>

But the string arrays are not copied to newArr. Can anyone help me out on this ?

like image 717
Manoj Shrestha Avatar asked Nov 04 '22 00:11

Manoj Shrestha


1 Answers

Something like:

var newArr = [ 
#foreach( $var in $arr )#if($foreach.index>  0),#end "$var" #end 
]; 
like image 82
xdazz Avatar answered Nov 12 '22 20:11

xdazz