简体   繁体   English

如何遍历Vue Js中Json数组中的子数组?

[英]How to loop through a sub array in a Json array in Vue Js?

I am having a JSON data array, I need to loop through outer as well as inner subarray and need to create a Table.我有一个 JSON 数据数组,我需要遍历外部子数组和内部子数组,并且需要创建一个表。 Below is the sample array下面是示例数组

    {
        class:'I',
        subDdiv:[
            {
                div: 'A',
                subjects:['Subject1','Subject2','Subject3']
            },
            {
                div: 'B',
                subjects:['Subject1','Subject2','Subject3']
            },   
        ]
}
    {
        class:'II',
        subDdiv:[
            {
                div: 'A',
                subjects:['Subject1','Subject2','Subject3']
            },
            {
                div: 'B',
                subjects:['Subject1','Subject2','Subject3']
            },   
        ]
}

Now I need to create a table with the row heading as Class and Div现在我需要创建一个表,其行标题为 Class 和 Div

labels :['class','div']

The code i written is not giving actual results,我写的代码没有给出实际结果,

<el-table :data="array" style="width: 100%">
  <el-table-column v-for="label in labels" :key="label"
   :prop="label"
   :label="label">
   </el-table-column>
 </el-table>

Note: i am using Element Ui Table - https://element.eleme.io/#/en-US/component/table注意:我正在使用 Element Ui 表 - https://element.eleme.io/#/en-US/component/table

I need to have a table like this我需要一张这样的桌子

我需要一张这样的桌子

But the table I got is但我得到的桌子是

在此处输入图像描述

Please help me to loop through inner subDiv and create the table.请帮助我遍历内部 subDiv 并创建表格。 code box - https://codesandbox.io/s/vigilant-wildflower-zgiq2?file=/src/App.vue代码框 - https://codesandbox.io/s/vigilant-wildflower-zgiq2?file=/src/App.vue

I have built a quick example with pure HTML tables, it should give you an idea about how you can achieve the same result with your UI components library too.我已经用纯 HTML 表构建了一个快速示例,它应该让您了解如何使用您的 UI 组件库实现相同的结果。

 new Vue({ el: "#app", data: { contents: [ { class:'I', subDdiv:[ { div: 'A', subjects:['Subject1','Subject2','Subject3'] }, { div: 'B', subjects:['Subject1','Subject2','Subject3'] }, ] }, { class:'II', subDdiv:[ { div: 'A', subjects:['Subject1','Subject2','Subject3'] }, { div: 'B', subjects:['Subject1','Subject2','Subject3'] }, ] } ] } })
 td, th { padding: 5px 20px; }
 <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script> <div id="app"> <table> <thead> <tr> <th>Class</th> <th>Div</th> </tr> </thead> <tbody> <template v-for="item in contents"> <tr v-for="subItem in item.subDdiv"> <td> {{ item.class }} </td> <td> {{ subItem.div }} </td> </tr> </template> </tbody> </table> </div>

To be able to loop the data in one loop (which is what the layout you are using require) you should flatten your data:为了能够在一个循环中循环数据(这是您使用的布局所需要的),您应该展平您的数据:

computed: {
    reducedArray() {
      return this.dataArray.reduce((prev, obj) => {
        let flatted = obj.subDdiv.map(item => {
          let subdiv = {};
          subdiv["class"] = obj.class;
          subdiv["div"] = item.div;
          return subdiv
        });
        return [...prev, ...flatted];
      }, []);
    }
  }

Then you can use your code as is, by looping the flatted array:然后你可以通过循环扁平数组来使用你的代码:

<el-table :data="reducedArray" style="width: 100%">
      <el-table-column v-for="label in labels" :key="label" :prop="label" :label="label"></el-table-column>
</el-table>

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

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