简体   繁体   English

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

[英]hierarchical data, how to loop all level and insert additional data into each node

I have hierarchical data in Javascript looks like below and I try to find the way add jsonStringify in each comments node, how to do it? 我在Javascript中有分层数据,如下所示,我尝试在每个comments节点中找到添加jsonStringify的方法,该怎么做?

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":
  }
};

add jsonStringfy 添加jsonStringfy
this only work with knowing how many level 这只能知道多少级别

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

for example above data have 2 branch, and the deepest level is 3 ( 例如上面的数据有2个分支,最深的是3个(
"comments" > "comments" > "comments", “评论”>“评论”>“评论”,
"comments" >"comments"), “评论”>“评论”),
I want to find each "comments" get the value like below 1 and apply to JSON.stringify function get result then modified the same node insert the result become 2 我想找到每个“注释”得到如下1的值并应用于JSON.stringify函数获取结果然后修改相同的节点插入结果变为2

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

I try to find the way if the data unknown how many level 我试图找到方法,如果数据未知有多少级别

It was answered before original question were modified with the remark to the different count numbers. 在原始问题被修改之前回答了对不同计数的注释。 Still waiting for author to elaborate about it. 仍在等待作者详细说明。

Source code: 源代码:

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

result: 结果:

[
    {
        "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\":[]}}]}"
        }
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM