manish thakur
manish thakur

Reputation: 820

How to loop JSON data

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"
    }
  ]
}

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;
<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>

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

Upvotes: 0

Views: 84

Answers (2)

Hardik Raval
Hardik Raval

Reputation: 3641

Try below snippet:

<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>

Upvotes: 1

HarshaHR
HarshaHR

Reputation: 73

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];
    }
}

Upvotes: 0

Related Questions