简体   繁体   English

下划线检查每个中的先前记录

[英]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 } 假设我有这样的记录:{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. 我想检查每个记录是否具有与实际级别相同的jump_level,因为我想告诉我是否不必打印下一个记录。

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: 好吧,您可以从字面上将其转换为JS代码:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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