如何循环遍历JSON数据

huangapple go评论78阅读模式
英文:

How to loop JSON data

问题

以下是您要翻译的内容:

我有一个json数据,我想使用vue.js制作一个Navbar,但我无法通过Navbar循环数据

这是我目前尝试使用我的JSON数据要做的事情,目前它是静态的,但我想循环遍历我拥有的数据

由于我的JSON数据是动态的,来自后端,我必须根据用户登录制作一个导航栏,以显示他们允许的内容。

我的JSON数据

{
  "Dashboard": [
    {
      "type": "Dashboard",
      "link": "Dashboard.html"
    }
  ],
  "Graphical Reports": [
    {
      "type": "Outlet Sales Summary Pai chart",
      "link": "Outlet Sales Summary Pai chart.html"
    },
    {
      "type": "Payment mode wise Graph layout",
      "link": "Payment mode wise Graph layout.html"
    },
    {
      "type": "Outlet wise Sales Area Chart",
      "link": "AreaChart.html"
    },
    {
      "type": "Outlet wise Sales Line Chart",
      "link": "LineChart.html"
    },
    {
      "type": "Top 20 Sold Items",
      "link": "Top20Items.html"
    },
    {
      "type": "Outlet Wise Quantity And Amount",
      "link": "OutletWiseQuantityandAmount.html"
    }
  ],
  "Tabular Reports": [
    {
      "type": "Date wise Outlet wise Sales Summary",
      "link": "Date wise Outlet wise Sales Summary.html"
    },
    {
      "type": "Date wise OL Wise Counter wise Sales",
      "link": "Date wise OL Wise Counter wise Sales.html"
    },
    {
      "type": "Hourly wise Sales Bet Dates",
      "link": "Hourly wise Sales Bet Dates.html"
    },
    {
      "type": "Outlet wise Date wise NoOfBills",
      "link": "Date Wise Ol Wise Bill Count.html"
    },
    {
      "type": "Hourly sales Outlet wise Date wise",
      "link": "Hourly sales Outlet wise Date wise.html"
    },
    {
      "type": "Percentage Contribution Outlet wise",
      "link": "PercentageolWise.html"
    },
    {
      "type": "Outlet wise Item wise Sales With Date",
      "link": "MRPL Store Sales With Date.html"
    }
  ],
  "Drill down Reports": [
    {
      "type": "Sales Drilldown Counterwise Bet Dates",
      "link": "Sales Drilldown Counterwise Bet Dates.html"
    },
    {
      "type": "Sales Drilldown Billwise Bet Dates",
      "link": "Sales Drilldown Billwise Bet Dates.html"
    },
    {
      "type": "Sales Drilldown Itemwise for Date",
      "link": "Sales Drilldown Itemwise for Date.html"
    },
    {
      "type": "Linked Sales Report Bet Dates",
      "link": "LinkingReport.html"
    }
  ],
  "Masters": [
    {
      "type": "Item Master",
      "link": "ItemMaster.html"
    }
  ],
  "Setup": [
    {
      "type": "Change Password",
      "link": "ChangePassword.html"
    },
    {
      "type": "User Admin",
      "link": "UserAdmin.html"
    }
  ],
  "Transactions": [
    {
      "type": "Indent Request",
      "link": "IndentWithCategoryWiseFilter.html"
    }
  ]
}

我已经尝试过了,但是不知道如何继续。

英文:

I have A json data from which I want to make a Navbar using vue .js, I am unable to loop the data through the Navbar

This is what I am trying to do with my JSON data currently it is static but I want to loop through the data I have

As my JSON data is dynamic , I am getting from backend, I have to make a navbar as per user login to show him / her whatever they are allowed for.

My JSON Data

    {
  "Dashboard": [
    {
      "type": "Dashboard",
      "link": "Dashboard.html"
    }
  ],
  "Graphical Reports": [
    {
      "type": "Outlet Sales Summary Pai chart",
      "link": "Outlet Sales Summary Pai chart.html"
    },
    {
      "type": "Payment mode wise Graph layout",
      "link": "Payment mode wise Graph layout.html"
    },
    {
      "type": "Outlet wise Sales Area Chart",
      "link": "AreaChart.html"
    },
    {
      "type": "Outlet wise Sales Line Chart",
      "link": "LineChart.html"
    },
    {
      "type": "Top 20 Sold Items",
      "link": "Top20Items.html"
    },
    {
      "type": "Outlet Wise Quantity And Amount",
      "link": "OutletWiseQuantityandAmount.html"
    }
  ],
  "Tabular Reports": [
    {
      "type": "Date wise Outlet wise Sales Summary",
      "link": "Date wise Outlet wise Sales Summary.html"
    },
    {
      "type": "Date wise OL Wise Counter wise Sales",
      "link": "Date wise OL Wise Counter wise Sales.html"
    },
    {
      "type": "Hourly wise Sales Bet Dates",
      "link": "Hourly wise Sales Bet Dates.html"
    },
    {
      "type": "Outlet wise Date wise NoOfBills",
      "link": "Date Wise Ol Wise Bill Count.html"
    },
    {
      "type": "Hourly sales Outlet wise Date wise",
      "link": "Hourly sales Outlet wise Date wise.html"
    },
    {
      "type": "Percentage Contribution Outlet wise",
      "link": "PercentageolWise.html"
    },
    {
      "type": "Outlet wise Item wise Sales With Date",
      "link": "MRPL Store Sales With Date.html"
    }
  ],
  "Drill down Reports": [
    {
      "type": "Sales Drilldown Counterwise Bet Dates",
      "link": "Sales Drilldown Counterwise Bet Dates.html"
    },
    {
      "type": "Sales Drilldown Billwise Bet Dates",
      "link": "Sales Drilldown Billwise Bet Dates.html"
    },
    {
      "type": "Sales Drilldown Itemwise for Date",
      "link": "Sales Drilldown Itemwise for Date.html"
    },
    {
      "type": "Linked Sales Report Bet Dates",
      "link": "LinkingReport.html"
    }
  ],
  "Masters": [
    {
      "type": "Item Master",
      "link": "ItemMaster.html"
    }
  ],
  "Setup": [
    {
      "type": "Change Password",
      "link": "ChangePassword.html"
    },
    {
      "type": "User Admin",
      "link": "UserAdmin.html"
    }
  ],
  "Transactions": [
    {
      "type": "Indent Request",
      "link": "IndentWithCategoryWiseFilter.html"
    }
  ]
}

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

new Vue({
  el: &#39;#app&#39;,
  data() {
    return {
      navData: {
        &quot;Setup&quot;: [{
            &quot;menu&quot;: &quot;Submenu-1&quot;
          },
          {
            &quot;route&quot;: &quot;submenu-2&quot;
          }
        ],
        &quot;Report&quot;: [{
            &quot;menue&quot;: &quot;subreport-1&quot;
          },
          {
            &quot;route&quot;: &quot;subreport-2&quot;
          }
        ]
      }

    }
  },

})

Vue.config.productionTip = false;
Vue.config.devtools = false;

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css&quot;&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=app&gt;

  &lt;nav class=&quot;navbar navbar-expand-sm bg-dark navbar-dark&quot;&gt;
    &lt;ul class=&quot;navbar-nav&quot;&gt;
      &lt;!-- Dropdown --&gt;
      &lt;li v-for=&quot;data in navData&quot;class=&quot;nav-item dropdown&quot;&gt;
        &lt;a class=&quot;nav-link dropdown-toggle&quot; href=&quot;#&quot; id=&quot;navbardrop&quot; data-toggle=&quot;dropdown&quot;&gt;
        {{Object.keys(data)}}
      &lt;/a&gt;
        &lt;div class=&quot;dropdown-menu&quot;&gt;
          &lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;menu&lt;/a&gt;

        &lt;/div&gt;

      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/nav&gt;
&lt;/div&gt;

<!-- end snippet -->

I have tried but, didn't how to go through this

答案1

得分: 1

以下是代码片段的翻译部分:

<!-- 开始代码片段:不隐藏,显示控制台,不使用Babel -->

<!-- 语言:HTML -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div id="app">

  <nav class="navbar navbar-expand-sm bg-dark navbar-dark">
    <ul class="navbar-nav">
      <!-- 下拉菜单 -->
      <li v-for="(data, menu) in navData" class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
          {{ menu }}
        </a>
        <div class="dropdown-menu">
          <a class="dropdown-item" href="#">{{ data[0].menu }}</a>
        </div>
      </li>
    </ul>
  </nav>
</div>

<script>
new Vue({
  el: '#app',
  data() {
    return {
      navData: {
        "Setup": [{
            "menu": "Submenu-1"
          },
          {
            "route": "submenu-2"
          }
        ],
        "Report": [{
            "menu": "subreport-1"
          },
          {
            "route": "subreport-2"
          }
        ]
      }
    }
  },
})

Vue.config.productionTip = false;
Vue.config.devtools = false;
</script>

<!-- 结束代码片段 -->

请注意,上述翻译中保留了HTML标记和代码的原始结构,只对文本进行了翻译。如果需要进一步的说明或有其他问题,请随时提出。

英文:

Try below snippet:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css&quot;&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=app&gt;
&lt;nav class=&quot;navbar navbar-expand-sm bg-dark navbar-dark&quot;&gt;
&lt;ul class=&quot;navbar-nav&quot;&gt;
&lt;!-- Dropdown --&gt;
&lt;li v-for=&quot;(data,menu) in navData&quot;class=&quot;nav-item dropdown&quot;&gt;
&lt;a class=&quot;nav-link dropdown-toggle&quot; href=&quot;#&quot; id=&quot;navbardrop&quot; data-toggle=&quot;dropdown&quot;&gt;
{{ menu }}
&lt;/a&gt;
&lt;div class=&quot;dropdown-menu&quot;&gt;
&lt;a class=&quot;dropdown-item&quot; href=&quot;#&quot;&gt;{{ data[0].menu }}&lt;/a&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;/div&gt;
&lt;script&gt;
new Vue({
el: &#39;#app&#39;,
data() {
return {
navData: {
&quot;Setup&quot;: [{
&quot;menu&quot;: &quot;Submenu-1&quot;
},
{
&quot;route&quot;: &quot;submenu-2&quot;
}
],
&quot;Report&quot;: [{
&quot;menu&quot;: &quot;subreport-1&quot;
},
{
&quot;route&quot;: &quot;subreport-2&quot;
}
]
}
}
},
})
Vue.config.productionTip = false;
Vue.config.devtools = false;
&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 0

var arr = [{ "id": "10", "class": "child-of-9" }, { "id": "11", "classd": "child-of-10" }];

for (var i = 0; i < arr.length; i++) {
var obj = arr[i];
for (var key in obj) {
var attrName = key;
var attrValue = obj[key];
}
}

英文:
var arr = [ {&quot;id&quot;:&quot;10&quot;, &quot;class&quot;: &quot;child-of-9&quot;}, {&quot;id&quot;:&quot;11&quot;, &quot;classd&quot;: &quot;child-of-10&quot;}];
for (var i = 0; i &lt; arr.length; i++){
var obj = arr[i];
for (var key in obj){
var attrName = key;
var attrValue = obj[key];
}
}

huangapple
  • 本文由 发表于 2020年1月3日 16:15:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定