javascript - How to use underscore.js sortBy -
javascript - How to use underscore.js sortBy -
i'm trying rearrange multidimensional array json file in underscore template can print out disease highest # of cases lowest # of cases
the problem comes sortby function not sorting arrays.
here illustration array i'm working with
the totalcases array looks like: [163, 134, 98, 118, 2, 167, 152, 102, 49, 4, 0, 0, 0, 1, 0, 0, 1265]
and sortby function is:
<% _.sortby(totalcases , function (num) { %> <%= num %> <% }); %>
it returns 163, 134, 98, 118, 2, 167, 152, 102, 49, 4, 0, 0, 0, 1, 0, 0, 1265
i don't know going wrong how i'm using sortby
you mixing view controller somehow. don't want rearrange array in the underscore template, before presenting it. reread docs sortby
, takes callback sort criteria or property name or so, don't want output in there.
<% var sortedcases = _.sortby(totalcases, function(case) { homecoming // sort criteria here, e.g. `case.occurences` }); %><ol><% _.each(sortedcases, function(case) { %> <li><%= case /* … */ %></li> <% } %></ol>
javascript underscore.js underscore.js-templating
Comments
Post a Comment