英文:
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: '#app',
data() {
return {
navData: {
"Setup": [{
"menu": "Submenu-1"
},
{
"route": "submenu-2"
}
],
"Report": [{
"menue": "subreport-1"
},
{
"route": "subreport-2"
}
]
}
}
},
})
Vue.config.productionTip = false;
Vue.config.devtools = false;
<!-- language: lang-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">
<!-- Dropdown -->
<li v-for="data in navData"class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
{{Object.keys(data)}}
</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">menu</a>
</div>
</li>
</ul>
</nav>
</div>
<!-- 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 -->
<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">
<!-- Dropdown -->
<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>
<!-- 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 = [ {"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];
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论