I am trying to convert a ruby array to javascript array in my rails application
@qs #=> [1, 4]
The javascript code is:
var js_array = [<%= @qs.to_json %>];
var arrayLength = js_array.length;
for (var i = 0; i < arrayLength; i++) {
alert(js_array[i]);
}
But I am getting the arrayLength
as 1
. The alert message in the loop display 1,4
once.
I have tried converting the array to string array, but there is no difference:
var new_array = js_array.map(String);
What I need: I should be able to loop through the javascript array and alert each element.