Javascript 在Mustache.js中渲染有限数量的对象

Javascript 在Mustache.js中渲染有限数量的对象,javascript,jquery,mustache,Javascript,Jquery,Mustache,我正在获取要在HTML页面上呈现的嵌套JSON对象。对象的长度和内容可能会有所不同,我想展示其中的一部分。我不知道如何使用Mustache.js实现它 我的对象如下所示: { "success": [ { "stopID": "123", "stopName": "ABC", "lines": [ { "lineRef": "3", "destinationID": "987", "destinationNa

我正在获取要在HTML页面上呈现的嵌套JSON对象。对象的长度和内容可能会有所不同,我想展示其中的一部分。我不知道如何使用Mustache.js实现它

我的对象如下所示:

{ "success": [
  {
    "stopID": "123",
    "stopName": "ABC",
    "lines": [
      {
        "lineRef": "3",
        "destinationID": "987",
        "destinationName": "DEF",
        "arrivals": [
          1490279520000,
          1490279460000,
          1490280180000
        ]
      }
    ]
  },
  {
    "stopID": "331",
    "stopName": "TVC",
    "lines": [
      {
        "lineRef": "3",
        "destinationID": "128",
        "destinationName": "NJH",
        "arrivals": [
          1490279160000,
          1490280180000
        ]
      },
      {
        "lineRef": "3V",
        "destinationID": "361",
        "destinationName": "KOI",
        "arrivals": [
          1490280360000
        ]
      }
    ]
  },
  {
    "stopID": "3601",
    "stopName": "LKG",
    "lines": [
      {
        "lineRef": "3",
        "destinationID": "128",
        "destinationName": "ABC",
        "arrivals": [
          1490279040000,
          1490280360000,
          1490280180000,
          1490280180000
        ]
      }
    ]
  }
]}
<script id="#timetables" type="text/template">
    {{#success}}
    <tr style = "">
       <th colspan="3" style="padding-left:25px;">{{stopName}}<span><sup style="padding-left:5px;font-weight:100"><small>{{stopID}}</sup></span></th>
       </tr>
       {{#lines}}
       <tr class="line-row">

          <td class="" style="border:none;width:10%;">{{lineRef}}</td>
          <td class="" style="border:none;width:30%;">{{destinationName}}</td>
          <td class="" style="border:none;width:60%;">{{arrivals}}</td>

        </tr>
        {{/lines}}



    {{/success}}
  </script>
我想显示最多两次到达,我如何在那里设置限制

我找到了,但没有多大帮助。我的代码如下:

{ "success": [
  {
    "stopID": "123",
    "stopName": "ABC",
    "lines": [
      {
        "lineRef": "3",
        "destinationID": "987",
        "destinationName": "DEF",
        "arrivals": [
          1490279520000,
          1490279460000,
          1490280180000
        ]
      }
    ]
  },
  {
    "stopID": "331",
    "stopName": "TVC",
    "lines": [
      {
        "lineRef": "3",
        "destinationID": "128",
        "destinationName": "NJH",
        "arrivals": [
          1490279160000,
          1490280180000
        ]
      },
      {
        "lineRef": "3V",
        "destinationID": "361",
        "destinationName": "KOI",
        "arrivals": [
          1490280360000
        ]
      }
    ]
  },
  {
    "stopID": "3601",
    "stopName": "LKG",
    "lines": [
      {
        "lineRef": "3",
        "destinationID": "128",
        "destinationName": "ABC",
        "arrivals": [
          1490279040000,
          1490280360000,
          1490280180000,
          1490280180000
        ]
      }
    ]
  }
]}
<script id="#timetables" type="text/template">
    {{#success}}
    <tr style = "">
       <th colspan="3" style="padding-left:25px;">{{stopName}}<span><sup style="padding-left:5px;font-weight:100"><small>{{stopID}}</sup></span></th>
       </tr>
       {{#lines}}
       <tr class="line-row">

          <td class="" style="border:none;width:10%;">{{lineRef}}</td>
          <td class="" style="border:none;width:30%;">{{destinationName}}</td>
          <td class="" style="border:none;width:60%;">{{arrivals}}</td>

        </tr>
        {{/lines}}



    {{/success}}
  </script>

{{{成功}
{{stopName}}{{stopID}
{{#行}
{{lineRef}}
{{destinationName}
{{arrivals}}
{{/行}
{{/成功}

MustacheJS没有逻辑性。它将无条件地给予你所给予的一切。也许可以看看如何使用
slice()创建要显示的数据子集(这是非破坏性的)。是的,我现在就是这么做的。在将数据馈送给Mustach之前对其进行某种预处理。必须使用嵌套for循环。有没有比这更好的方法呢。我不明白这一点