Handling the last list item differently in rudders # each block

Possible duplicate:
How do I add a separator between elements in a {{#each}} loop, except for the last element?

For the simplest case, let's say I have a list of names and I want them to appear with commas in between. I could do

{{#each name in names }} {{name}}, {{/each}}

      

which will then produce the last

John, Paul, George, Ringo,

      

with a trailing comma, which is not needed. I can figure out how to handle this by adding commas to my controller functions, but that seems like an awkward and awkward violation of MVC separation. Is there a steering wheel-only way to detect when you are dealing with the last item and adjust accordingly?

+3


source to share


2 answers


Javascript has a function .join

that you can most likely use:



['John', 'Paul', 'George', 'Ringo'].join(', '); //John, Paul, George, Ringo

      

+1


source


Another way if you can't use Join.



var arr = ['John', 'Paul', 'George', 'Ringo'];
var names="";
$.each(arr, function(i,val) { 

if (i < arr.length-1)
  names += val + ","
else
 names += val
 });
alert (names);

      

0


source







All Articles