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?
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
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 (
"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
"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
jsonStringify()function to its value? And if so, what do you want to the result of the stringifying?JSON.stringify()to its value, then append the result to the same "comments" node