【问题标题】:hierarchical data, how to loop all level and insert additional data into each node分层数据,如何循环所有级别并将附加数据插入每个节点
【发布时间】:2016-05-13 09:11:50
【问题描述】:

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

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

例如上面的数据有2个分支,最深的层次是3(
"cmets" > "cmets" > "cmets",
"cmets" >"cmets"),
我想找到每个“cmets”获取如下1的值并应用于JSON.stringify函数获取结果然后修改相同的节点插入结果变为2

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

如果数据不知道有多少级,我会尝试寻找方法

【问题讨论】:

  • 完全不清楚你在这里问什么。
  • 您要查找每个名为“cmets”的节点,然后将jsonStringify() 函数应用于其值吗?如果是这样,你想要字符串化的结果是什么?
  • @JohnHascall 感谢您的回复,是的,我想找到每个节点“cmets”并将JSON.stringify() 应用于其值,然后将结果附加到相同的“cmets”节点
  • 还是不明白,可能是你用的措辞。你能举一个输入和输出的例子吗?你想序列化某事吗?还是这是您实现目标的方法?你想以什么结束?对象还是字符串?
  • @Thomas 请看我的更新

标签: javascript json loops hierarchy hierarchical-data


【解决方案1】:

在修改原始问题之前回答了不同计数的注释。 还在等待作者详细说明。

源代码:

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

【讨论】:

  • 感谢回复,但这似乎只适用于知道多少级 cmets
  • 对不起,我当时不太明白。那么你需要如何让机制工作呢?
  • 你能用已经计算好的jsonStringify字段显示你想要的最终JSON吗?
  • 你回答的结果是我想要的数据格式,但我的问题是让函数工作不必知道分层数据中有多少级别。
  • 哦,我的天,很抱歉浪费你的时间,我只看了你的代码而不是测试,错过了里面的函数调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多