While the extra comma shouldn't harm anything (JS does not care in all modern browsers), you'd better off encoding this in JSON beforehand:
// Controller Code
public String getFromFields() {
return JSON.serialize(fromFields);
}
// JS Code
var myarray = {!fromFields};
Here's a fully functional example:
Controller
public with sharing class arrayjson {
string[] fromFields = new List<String>{'Hello','Master','Fear'};
public String getFromFields() {
return JSON.serialize(fromFields);
}
}
Page
<apex:page controller="arrayjson">
<script>
var fromFields = {!fromFields};
for(var field in fromFields) {
alert(fromFields[field]);
}
</script>
</apex:page>
Using apex:repeat
If you absolutely want to build the array using repeat:
var fromFields = [];
<apex:repeat value="{!fromFields}" var="field">
fromFields.push("{!field}");
</apex:repeat>
fromFields.length
property, it returns "3". – eyescream May 29 '14 at 19:07