分层数据,如何循环所有级别并将其他数据插入每个节点

时间:2016-02-03 23:02:46

标签: javascript json loops hierarchy hierarchical-data

我在Javascript中有分层数据,如下所示,我尝试在每个comments节点中找到添加var o = { "comments": { "count": 2, "data": [ { "text": "..", "comments": { "count": 1, "data": [ { "text": "..", "comments": { "count": 0, "data": [], // "jsonStringify": } }, ], // "jsonStringify": } }, { "text": "..", "comments": { "count": 0, "data": [], // "jsonStringify": } }, ], // "jsonStringify": } }; 的方法,该怎么做?

jsonStringfy

添加var jsonStringify = JSON.stringify(o.comments); o.comments.jsonStringify = jsonStringify; for (var i = 0; i < o.comments.data.length; i++) { var jsonStringify = JSON.stringify(o.comments.data[i].comments); o.comments.data[i].comments.jsonStringify = jsonStringify; }
这只能知道多少级别

1

例如上面的数据有2个分支,最深的是3个(
&#34;注释&#34; &GT; &#34;注释&#34; &GT; &#34;注释&#34 ;,
&#34;注释&#34; &GT;&#34;注释&#34),
我想找到每个&#34;评论&#34;得到如JSON.stringify以下的值并应用于2函数获取结果然后修改相同的节点插入结果变为1 "comments": { "count": 0, "data": [] } 2 "comments": { "count": 0, "data": [], "jsonStringify": "{\"count\":0,\"data\":[]}" }

{{1}}

我试图找到数据未知的方式

1 个答案:

答案 0 :(得分:4)

在原始问题被修改之前回答了对不同计数的评论。 仍在等待作者详细说明。

源代码:

var o = {
  "comments": {
    "count": 2,
    "data": [
      {
        "text": "..",
        "comments": {
          "count": 1,
          "data": [
            {
              "text": "..",

              "comments": {
                "count": 0,
                "data": [],
              }
            },
          ]
        }
      },
      {
        "text": "..",
        "comments": {
          "count": 0,
          "data": []
        }
      }
    ]
  }
};

function jsonStringify(array){
  for(var i=0;i<array.length;i++){
    var ar = array[i];
    ar.comments.jsonStringify = JSON.stringify(ar.comments);
    ar.comments.data = jsonStringify(ar.comments.data);
    array[i] = ar;
  }
  return array;
}

var result = jsonStringify([o]);

console.log( JSON.stringify(result,null,'\t') );

结果:

[
    {
        "comments": {
            "count": 2,
            "data": [
                {
                    "text": "..",
                    "comments": {
                        "count": 1,
                        "data": [
                            {
                                "text": "..",
                                "comments": {
                                    "count": 0,
                                    "data": [],
                                    "jsonStringify": "{\"count\":0,\"data\":[]}"
                                }
                            }
                        ],
                        "jsonStringify": "{\"count\":1,\"data\":[{\"text\":\"..\",\"comments\":{\"count\":0,\"data\":[]}}]}"
                    }
                },
                {
                    "text": "..",
                    "comments": {
                        "count": 0,
                        "data": [],
                        "jsonStringify": "{\"count\":0,\"data\":[]}"
                    }
                }
            ],
            "jsonStringify": "{\"count\":2,\"data\":[{\"text\":\"..\",\"comments\":{\"count\":1,\"data\":[{\"text\":\"..\",\"comments\":{\"count\":0,\"data\":[]}}]}},{\"text\":\"..\",\"comments\":{\"count\":0,\"data\":[]}}]}"
        }
    }
]