在根节点及其所有子节点上以JSON格式附加键值

时间:2015-09-09 05:45:36

标签: javascript html dom

这是我正在处理的JSON对象:

var r={
"id": "A",
"name": "Analysis",
"url": "A.html",
"root": true,
    "children": [

{
    "id": "B",
    "name": "Introduction",
    "url": "B.html",
    "root": true,
    "children": [{
        "id": "C",
        "name": "Creating",
        "url": "c.html#I1"
    }, {
        "id": "D",
        "name": "Running",
        "url": "d.html#I2"
    }]
},

{
    "id": "E",
    "name": "Transient Analysis",
    "url": "E.html",
    "root": true,
        "children": [{
        "id": "F",
        "name": "RC",
        "url": "F.html#T1"
    }, {
        "id": "G",
        "name": "RLC",
        "url": "G.html#T2"
    }]
}

]
}

我希望这是另一个键值

path="home.html" 

它不仅应该附加到根节点,还应该附加到它的所有子节点。

所以看起来应该是这样。

 var r=  {
"id": "A",
"name": "Analysis",
"url": "A.html",
"path":"home.html",
"root": true,
    "children": [

{
    "id": "B",
    "name": "Introduction",
    "url": "B.html",
    "root": true,
    "path":"home.html",
    "children": [{
        "id": "C",
        "name": "Creating",
        "url": "c.html#I1",
        "path":"home.html"
    }, {
        "id": "D",
        "name": "Running",
        "url": "d.html#I2",
        "path":"home.html"
    }]
},

{
    "id": "E",
    "name": "Transient Analysis",
    "url": "E.html",
    "root": true,
    "path":"home.html",
        "children": [{
        "id": "F",
        "name": "RC",
        "url": "F.html#T1",
        "path":"home.html"
    }, {
        "id": "G",
        "name": "RLC",
        "url": "G.html#T2",
        "path":"home.html"
    } ]
}

]
}

到目前为止,我试过这个:

r.path={"home.html"}

var to_concatjson = JSON.parse(r);
to_concatjson["path"] = {"home.html"};

但两者都不适合我。

上述问题的进一步修改:

假设我正在从我的文件1加载我的json内容

文件1看起来像

[ 

{
   "id":"a", "name":"a","children":[
          {     "id":"b", "name":"b","children":[    

                                {     "id":"b", "name":"b"},
                                 {     "id":"c", "name":"c"},
                                 {     "id":"d", "name":"d"}
                                 ]
           },

       {     "id":"e", "name":"e","children":[    

                                {     "id":"f", "name":"f"},
                                 {     "id":"g", "name":"g"},
                                 {     "id":"h", "name":"h"}
                                 ]
           },



]
}
]

我的json文件的内容加载在result [0]中。当我调用addPath时:

result[0]=addPath(result[0]);

它向我显示一个错误,“forEach”将不起作用,因为它不是对象的属性。

2 个答案:

答案 0 :(得分:2)

使用此代码执行此操作: -

function addPath(obj){
    if(!obj.hasOwnProperty('path')){
        obj.path = 'home.html';
    }
    if(obj.hasOwnProperty('children')){
        obj.children.forEach(function(obj1){
            obj1 = addPath(obj1);
        });
    }
    return obj;
}

r =  addPath(r);

答案 1 :(得分:0)

我知道我的方式不是最佳的,但它正在发挥作用:

我通过 .replace(...) (没有循环)

添加新项目
var r  = JSON.parse(JSON.stringify(r).replace(new RegExp("\"url\"", 'g'), '"path": "home.html", "url"'));
console.log(r);



var r={
"id": "A",
"name": "Analysis",
"url": "A.html",
"root": true,
    "children": [

{
    "id": "B",
    "name": "Introduction",
    "url": "B.html",
    "root": true,
    "children": [{
        "id": "C",
        "name": "Creating",
        "url": "c.html#I1"
    }, {
        "id": "D",
        "name": "Running",
        "url": "d.html#I2"
    }]
},

{
    "id": "E",
    "name": "Transient Analysis",
    "url": "E.html",
    "root": true,
        "children": [{
        "id": "F",
        "name": "RC",
        "url": "F.html#T1"
    }, {
        "id": "G",
        "name": "RLC",
        "url": "G.html#T2"
    }]
}

]
}
var r  = JSON.parse(JSON.stringify(r).replace(new RegExp("\"url\"", 'g'), '"path": "home.html", "url"'));
console.log(r);