hierarchical data, how to loop all level and insert additional data into each node
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\":[]}}]}" } }]