Javascript 操纵对象结构

Javascript 操纵对象结构,javascript,jquery,angularjs,object,javascript-objects,Javascript,Jquery,Angularjs,Object,Javascript Objects,我发现了一个将如下对象转化为可展开树的: $scope.dataForTheTree = [ { "name" : "Joe", "age" : "21", "children" : [ { "name" : "Smith", "age" : "42", "children" : [] }, { "name" : "Gary", "age" : "21", "children" : [ { "name" : "Jenifer", "

我发现了一个将如下对象转化为可展开树的:

$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:[{}]}])

这就是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>

.
.
.

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

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

输入:

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":[

        ]
     }
  ]
}]
结果:

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":[

        ]
     }
  ]
}]

我一直试图让它工作,但它返回的对象丢失了很多网站。下面是我如何实现它的:我编辑了您提供的数据以添加更多层,我注意到该对象缺少Subite2:这可能是我无法使其工作的原因。@Batman ooops,抱歉。我的提琴有一个打字错误(在
站点.children
循环中缺少括号)。更新版本请参见此处:我为花了多长时间才找到更改而感到尴尬。现在试一试,效果很好。非常感谢你的帮助。我试过了,但没用。