Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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

like image 544
user1575921 Avatar asked Jun 22 '26 07:06

user1575921


1 Answers

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\":[]}}]}"
        }
    }
]
like image 73
Eugene Hauptmann Avatar answered Jun 24 '26 19:06

Eugene Hauptmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!