分层数据,如何循环所有级别并将附加数据插入每个节点
hierarchical data, how to loop all level and insert additional data into each node
我在 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(
"comments" > "comments" > "comments",
"comments" >"comments"),
我想找到每个 "comments" 得到如下值 1
并应用于 JSON.stringify
函数得到结果然后修改相同的节点插入结果成为 2
1
"comments": {
"count": 0,
"data": []
}
2
"comments": {
"count": 0,
"data": [],
"jsonStringify": "{\"count\":0,\"data\":[]}"
}
数据不明多少级我自己找方法
在修改原始问题之前回答了不同计数的备注。
还等作者详细说呢
源代码:
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\":[]}}]}"
}
}
]
我在 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(
"comments" > "comments" > "comments",
"comments" >"comments"),
我想找到每个 "comments" 得到如下值 1
并应用于 JSON.stringify
函数得到结果然后修改相同的节点插入结果成为 2
1
"comments": {
"count": 0,
"data": []
}
2
"comments": {
"count": 0,
"data": [],
"jsonStringify": "{\"count\":0,\"data\":[]}"
}
数据不明多少级我自己找方法
在修改原始问题之前回答了不同计数的备注。 还等作者详细说呢
源代码:
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\":[]}}]}"
}
}
]