在vue中嵌套数组循环?

时间:2017-08-16 17:54:55

标签: vue.js vuejs2

如何使用v-for循环使用下面的JSON对象? 我想循环所有ID'数字,以及每个数字内的所有项目,并将其全部显示在列表中... 我知道我可以使用v-for="item in system_events"轻松循环所有system_events 但是,如何循环所有不同的ID /号码以及所有内容?

我的JSON看起来像:

{
    "system_events": {
        "1013": [{
                "id": 25899,
                "timestamp": "2017-08-15T21:26:42Z",
                "type": "alarm",
                "code": 190,
                "title": "",
                "description": "",
                "appeared": "2017-08-15T21:26:40Z",
                "disappeared": null,
                "acknowlegded": null,
                "solved": null,
                "system_name": "Randers pr 44b sidste station"
            }, {
                "id": 26157,
                "timestamp": "2017-08-15T21:32:17Z",
                "type": "alarm",
                "code": 190,
                "title": "",
                "description": "",
                "appeared": "2017-08-15T21:32:06Z",
                "disappeared": null,
                "acknowlegded": null,
                "solved": null,
                "system_name": "Randers pr 44b sidste station"
            }
        ],
        "1015": [{
                "id": 23777,
                "timestamp": "2017-08-15T20:38:08Z",
                "type": "alarm",
                "code": 191,
                "title": "",
                "description": "",
                "appeared": "2017-08-15T20:38:00Z",
                "disappeared": null,
                "acknowlegded": null,
                "solved": null,
                "system_name": "Favrskov Svenstrup gyvelvej"
            }, {
                "id": 23779,
                "timestamp": "2017-08-15T20:38:08Z",
                "type": "alarm",
                "code": 190,
                "title": "",
                "description": "",
                "appeared": "2017-08-15T20:37:58Z",
                "disappeared": null,
                "acknowlegded": null,
                "solved": null,
                "system_name": "Favrskov Svenstrup gyvelvej"
            }
        ]
    }
}

2 个答案:

答案 0 :(得分:0)

除了Bert的答案之外,我会说你可以使用template标签,以便不渲染主框架。例如,如果您只想要子项目的干净HTML ul>li,这很有用。

const vue = new Vue({
  el: "#app",
  data: {
    system_events: {"1013":[{"id":25899},{"id":26157}],"1015":[{"id":23777},{"id":23779}]}
  }
});
<script src="https://unpkg.com/vue@2.2.6/dist/vue.js"></script>

<div id="app">
  <ul>
    <template v-for="item in system_events">
      <li v-for="event in item">{{ event.id }}</li>
    </template>
  </ul>
</div>

答案 1 :(得分:0)

另一种方法是使用计算属性来简化数据结构:

new Vue({
  el: '#app',
  data: {
    system_events: {"1013":[{"id":25899},{"id":26157}],"1015":[{"id":23777},{"id":23779}]}    
  },
  computed: {
    allEvents: function() {
      return Object.values(this.system_events)
        .reduce((a, b) => a.concat(b), [])
    }
  }
})

这允许您在模板中使用一个简单的循环,这使得标记的结构更清晰:

<script src="https://unpkg.com/vue"></script>

<div id="app">
  <ul>
    <li v-for="event in allEvents">{{event.id}}</li>
  </ul>
</div>

如果您正在处理大型数据集,和/或数据经常发生变异,您可能希望测试这两种方法的性能。我不知道哪一个更好。

JSFiddle:https://jsfiddle.net/em33av7j/