【问题标题】:How can I get the index of an external JSON key or value using mustache.js如何使用 mustache.js 获取外部 JSON 键或值的索引
【发布时间】:2018-04-13 02:51:40
【问题描述】:

我正在循环遍历这个元素:

{{#video}}
<li id="video{{type}}" onclick="FWDRL.show(\'{{lbid}}colTV\', {{index}});" data-url="{{url}}" data-poster-path="{{cover}}" data-thumbnail-path="{{cover}}" data-width="800" data-height="450"><h4><i class="fa fa-play-circle-o"></i> {{type}}</h4><p class="description"></p></li>
{{/video}}

这会抓取我的外部 JSON 文件中的每个视频实例。但是,我需要在{{index}} 标记中添加这些元素的索引值。我该怎么做?

【问题讨论】:

  • 小胡子不能那样做。可以使用my fork of mustache.js{INDEX} mini-tag,或者解析后修改JSON的对象,在数据中添加索引道具。
  • @dandavis 感谢您的回答。我将索引道具添加到 JSON 数据中,但我觉得那是在欺骗逻辑,并且没有达到应有的效率;不过效果很好。这个 mostache.js 看起来很漂亮。我一定会检查出来的。再次感谢!
  • 调整传入的数据是在 mustache 中做很多事情的唯一方法。如果您不想更改实时值,可以克隆它。当我决定将小胡子分叉成 mostache 时,我处于无法修改它的情况......

标签: javascript arrays json indexing mustache


【解决方案1】:

一个是,你需要在你的 JSON 对象中定义一个属性 index

var counter = 1;
obj['index'] = function(){
    return counter++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多