简体   繁体   中英

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

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>
<%     }
   }); %>

只需将前一个room的状态存储在一个“全局”变量中,就可以使用该设置,每次咨询该状态时都使用默认基本值进行设置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM