Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

Underscore check previous record in each

I have an app in backbone and a template with underscore. I want to know if is possible to check a value inside a each of the previous record or something to check.
Suppose that I have record like this: { id: 1, level:1, jump_level:2 }, { id: 2, level:2, jump_level:0 }

Into my each I want to check if previous record has the same jump_level of the actual level because I want to tell that if i have to not print next record.

This is a piece of my template:

<div>
<% _.each(room2, function(room) { %>
     //I would like to write an if like this:
     // if exist previous room -> check if jump_level == level if yes don't print span
     <span> <%= room.attributes.id %></span>                                           
<% }); %>
</div>

Is possible? Thanks

Upvotes: 0

Views: 170

Answers (2)

Bergi
Bergi

Reputation: 664599

Well you can quite literally translate that to JS code:

<% _.each(room2, function(room, i) {
       if ( !(i>0 && room2[i-1].jump_level == room.jump_level) ) { %>
           <span> <%= room.attributes.id %></span>
<%     }
   }); %>

Upvotes: 1

DRC
DRC

Reputation: 5048

just store the state of the previous room in a "global" variable, you could use that each consulting that state setted with a default base value the first time.

Upvotes: 0

Related Questions