HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Is there a better way to output a javascript array from ASP.net?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fromjavascriptarraywayoutputbetternetthereasp

Problem

I often run into the problem of producing a javascript array on an ASP.net page from an IEnumerable and I was wondering if there was an easier or clearer way to do it than


        ,
      
     ''
  %>


The whole thing is made much more verbose because of IE's inability to handle a hanging comma in an array or object.

Solution

With a bit of help from System.Linq this becomes quite easy.

var array = [  "'" + v.ToString() + "'").ToArray())  
%> ];

Code Snippets

var array = [ <%= 
    string.Join(",", items.Select(v => "'" + v.ToString() + "'").ToArray())  
%> ];

Context

StackExchange Code Review Q#881, answer score: 6

Revisions (0)

No revisions yet.