Javascript / Jquery遍历对象内的对象值

时间:2018-05-29 16:31:21

标签: javascript jquery html

var object =   [{
      "mainMenu": "MainMenu1",
      "Submenu": [
        {
          "SubMenu1": [
            { "name": "SubMenu1 page1", "access": "Read" },
            { "name": "SubMenu1 page2", "access": "Write" }
          ]
        },
        {
          "SubMenu2": [
            { "name": "SubMenu2 page1", "access": "Read" },
            { "name": "SubMenu2 page2", "access": "Write" }
          ]
        },
        {
          "SubMenu3": [
            { "name": "SubMenu3 page1", "access": "Read" },
            { "name": "SubMenu3 page2", "access": "Write" }
          ]
        }
      ]
    },
    {
      "mainMenu": "MainMenu1",
      "Submenu": [
        {
          "SubMenu1": [
            { "name": "SubMenu1 page1", "access": "Read" },
            { "name": "SubMenu1 page2", "access": "Write" }
          ]
        },
        {
          "SubMenu2": [
            { "name": "SubMenu2 page1", "access": "Read" },
            { "name": "SubMenu2 page2", "access": "Write" }
          ]
        },
        {
          "SubMenu3": [
            { "name": "SubMenu3page1", "access": "Read" }
          ]
        }
      ]
    }]

我有一个看起来像上面的阵列。我想遍历每个对象并获取值。如何在Javascript / jQuery中完成?

我尝试使用:

for (var key1 in object) {
  var e1 = object[key1];
  for (var key2 in e1) {
    var e2 = e1[key2];
    //console.log(e2);
    for (var key3 in e2) {
      console.log(e2);
      console.log(e2[key3]);
      $(".div1").append("<div class='main'><h4>"+e1+"</h4><ul><li>"+e2[key2]+"<ul><li>Sub Menu1</li><li>Sub Menu2</li></ul></li><li>"+e2[key2]+"<ul><li>Sub Menu1</li><li>Sub Menu2</li></ul></li></ul>");

    }
  }
}

我在PHP代码中找到了以下链接,我需要在Javascript / Jquery

中进行

PHP code Link

0 个答案:

没有答案