需要帮助迭代复杂的Json(没有jquery)

时间:2015-05-08 19:26:40

标签: javascript ajax json symfony

我需要创建一个symfony2包,用于从YAML文件生成侧栏

我创建了这个YAML结构

Sidebar:
  - Frontpage:
        - Dashboard:
            _icon: 'icon-home'
            _route: 'link'
  - Actions:
          - My_Likes:
              _icon: 'icon-dislike'
              _route: 'link'
          - My_Dislikes:
              _icon: 'icon-home'
              _route: 'link'
  - Interests:
          - Add_Interest:
              _icon: 'icon-home'
              _route: 'link'

将此JSON作为响应返回。

{
  "Sidebar": [
    {
      "Frontpage": [
        {
          "Dashboard": {
            "_icon": "icon-home",
            "_route": "link"
          }
        }
      ]
    },
    {
      "Actions": [
        {
          "My_Likes": {
            "_icon": "icon-dislike",
            "_route": "link"
          }
        },
        {
          "My_Dislikes": {
            "_icon": "icon-home",
            "_route": "link"
          }
        }
      ]
    },
    {
      "Interests": [
        {
          "Add_Interest": {
            "_icon": "icon-home",
            "_route": "link"
          }
        }
      ]
    }
  ]
}

使用ajax,json将返回'数据'客户端的变量

Sidebar.model.request(function(data)
{ 
    for(var a=0; a< data.Sidebar.length; a++ )
    {
       console.log(data.Sidebar[a]);
    }
});

我需要找到一种方法来遍历父母并找到相应的孩子。 我只需要帮助创建for循环,所以使用console.log(data [stuff])的解决方案;就足够了

编辑: 这是经过调整的Daniel Rosano代码片段

    Sidebar.model.request(function(data)
    { 
        //Get Sidebar items
        var SidebarItems = data.Sidebar;

        //Find Categories in Sidebar Items
        for(var a=0; a< SidebarItems.length; a++ )
        {
            var category = SidebarItems[a];

            //Get Category name and append it to sidebar
            var category_name = getSubitemName(category);
            Sidebar.view.renderCategory(category_name);

            //find subitems in categories
            for(var b=0; b < category[category_name].length; b++)
            {
                var button = category[category_name][b];
                var button_name = getSubitemName(button);

                var button_attributes = button[button_name];

                console.log(button_attributes['_icon']);
                Sidebar.view.renderButton(button_name);
            }
        }

        function getSubitemName(parent)
        {
            for(child in parent)
            {
                return child.toString();
            }
        }
    });

这是结果,谢谢Daniel

Obviously incomplete but this is the result

3 个答案:

答案 0 :(得分:3)

我知道你已经接受了答案,但我已经写了这篇文章然后在张贴之前分心了所以我想我还是会分享它。

它是一个递归迭代器,遍历任何传入的数组或对象。它还跟踪任何特定项目和级别的“路径”(主要用于说明目的,但也可能是其他情况)也有用)。对于传入的任何数据都可以使用的通用迭代器,几乎必须递归才能处理任意深度。

function iterate(item, path, level) {
    level = level || 0;
    path = path || "root";
    if (typeof item === "object") {
        if (Array.isArray(item)) {
            out("iterating array: " + path, level);
            for (var i = 0; i < item.length; i++) {
                iterate(item[i], path + "[" + i + "]", level + 1);
            }
        } else {
            out("iterating object: " + path, level);
            for (var prop in item) {
                // skip any properties on the prototype
                if (item.hasOwnProperty(prop)) {
                    iterate(item[prop], path + "." + prop, level + 1);
                }
            }
        }
    } else {
        // leaf level property
        out(path + " = " + item, level);
    }
}

工作演示,了解路径和级别的工作原理:http://jsfiddle.net/jfriend00/k8aosv59/

答案 1 :(得分:1)

不确定这是否是您需要的

for (var a = 0; a < t.Sidebar.length; a++) {
  var children = t.Sidebar[a];
  for (k in children) {
    var subchild = children[k];
    for (m in subchild) {
      var sschild = subchild[m];
      for (n in sschild) {
         // menu variable has the inner childs (having "_icon" and "_route")
         var menu = sschild[n];
         console.log(menu._icon+ " "+menu._route);
      }
    }
  }
}

希望有所帮助

答案 2 :(得分:1)

您可以递归地执行此操作:

function iterate(obj) {
    console.log(obj);

    for (var key in obj) {

        var items = obj[key];

        for(var i=0,l=items.length;i<l;i++) {
            iterate(items[i]);
        }
    }
}

iterate(data);

Fiddle