简体   繁体   English

在二维数组上迭代时获取外部for循环的索引

[英]Getting index of outer for loop when iterating over 2 dimensional array

I am iterating over a 2 dimensional array in a jsrender template. 我正在jsrender模板中的二维数组上进行迭代。 I would like to get indexes of both 'for' loops (inner and outer). 我想获得两个“ for”循环(内部和外部)的索引。 Is it possible? 可能吗? I know I can get the index of the current (inner) 'for' loop using #index variable. 我知道我可以使用#index变量获取当前(内部)“ for”循环的索引。 But how can I get external index? 但是如何获取外部索引? Example

  {{for cachedImages}}
  <tr>
    {{for #data}}
    <td><img src="/Cache/{{:#outerIndex}}/{{:#index}}"/></td>
    {{/for}}
  {{/for}}

You can step up through parents, and use: 您可以加强与父母的联系,并使用:

{{for cachedImages}}
  <tr>
    {{for #data}}
      <td><img src="/Cache/{{:#parent.parent.index}}/{{:#index}}"/></td>
    {{/for}}
  </tr>
{{/for}}

Alternatively you can define a contextual index variable in the outer loop that you can access from within nested contexts: 或者,您可以在外部循环中定义上下文索引变量,您可以从嵌套上下文中访问该变量:

{{for cachedImages}}
  <tr>
    {{for #data ~outerIndex=#index}}
      <td><img src="/Cache/{{:~outerIndex}}/{{:#index}}"/></td>
    {{/for}}
  </tr>
{{/for}}

See also some other replies such as: JsRender Access parent index in nested template 另请参见其他一些答复,例如: 嵌套模板中的JsRender Access父索引

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

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