操纵对象结构

时间:2014-02-23 17:25:45

标签: javascript jquery angularjs object javascript-objects

我找到一个angular directive,它接受​​一个像下面这样的对象并将其变成一个可扩展的树:

$scope.dataForTheTree =
[
    { "name" : "Joe", "age" : "21", "children" : [
        { "name" : "Smith", "age" : "42", "children" : [] },
        { "name" : "Gary", "age" : "21", "children" : [
            { "name" : "Jenifer", "age" : "23", "children" : [
                { "name" : "Dani", "age" : "32", "children" : [] },
                { "name" : "Max", "age" : "34", "children" : [] }
            ]}
        ]}
    ]},
    { "name" : "Albert", "age" : "33", "children" : [] },
    { "name" : "Ron", "age" : "29", "children" : [] }
];

我认为这是JSON,因为属性是文本元素,但我已尝试使用属性而不是字符串的指令,它仍然有效。所以name代替"name"。 我得到了这段代码,它循环遍历XML并返回一个如下所示的对象:

    {
        homesite: { //I need to remove the properties homesite, subsite1, subsite2, etc
                  url: url,
                  title: title,
                  children: { //the child objects need to be wrapped in an array [{},{}...]
                             {
                              subsite1: url: url,
                                        title: title,
                                        children:{}
                             }{
                              subsite2: url: url,
                                        title: title,
                                        children:{}
                             }
                            }
                   }
    }

它基本上是我网站的地图,每个子元素都是子网站及其信息。

这是我用来解析XML并创建站点地图的函数:

    var map = {}; //init the map
    var web = $(xData.responseXML).find("Web").map(function () {
        return $(this).attr('Url');
    });
    var webTitle = $(xData.responseXML).find("Web").map(function () {
        return $(this).attr('Title');
    });  

    // create map
    //goes through the XML to get each site address
          var item = web[i], 
    //goes through XML to get each site Title
           title = webTitle[i], 
    //Breaks down URL to identify subsites
           parts = item.split('/'), 
    //Always top level site
           domain = parts.splice(0, 3).join('/'),
           current;  

    //Checks if the map object is empty, sets collection properties
    if ($.isEmptyObject(map)) map = {url:domain, title:title ,children:[]};
            current = map[domain].children;

    for (var index in parts) {
        var part = parts[index];
        if (!current[part]) {
            current[part] = {url:domain+'/'+parts.slice(0,index+1).join('/'), title:title, children:{}};
        }
        current = current[part].children;
    }
}
siteMap = [map]

问题:我很难弄清楚如何修改它以返回对象结构,就像指令需要它一样。所有信息都在那里,但结构不是。例如,子项包含在对象而不是数组中,这些站点都包含在属性(例如,site1:{})中,与(例如[{title:site1,children:[{}]}])< / p>

这就是XML的样子:

<Webs xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <Web Title="Site1_Name" Url="http://Server_Name/sites/Site_Name" />
   <Web Title="Site2_Name" Url="http://Server_Name/sites/Site_Name/Subsite_1" />
   <Web Title="Site3_Name" Url="http://Server_Name/sites/Site_Name/Subsite_1/Subsite_2" />
   .
   .
   .
</Webs>

1 个答案:

答案 0 :(得分:2)

您必须编写一个递归函数,将您的格式转换为目标格式。像这样:

function getNodeFromSite(site) {
    var node = { url: site.url, title: site.title };
    if (site.children) {
        node.children = [];
        for (var childName in site.children) {
            var child = site.children[childName];
            node.children.push(getNodeFromSite(child));
        }
    }
    return node; 
}

var tree = [];
for (var siteName in data) {
    var site = data[siteName];
    tree.push(getNodeFromSite(site));
}

Fiddle demo

<强>输入:

var data = {
    homesite: {
        url: "http://www.mysite.com",
        title: "Home",
        children: {
            subsite: {
                url: "http://www.mysite.com/subsite",
                title: "Sub-site",
                children: { }
            }
        }
    }
};

<强>结果:

[{
  "url":"http://www.mysite.com",
  "title":"Home",
  "children":[
     {
        "url":"http://www.mysite.com/subsite",
        "title":"Sub-site",
        "children":[

        ]
     }
  ]
}]