3

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

5
  • 2
    It is not at all clear what you are asking here. Commented Feb 3, 2016 at 23:06
  • Do you want to find each node named 'comments' and then apply the jsonStringify() function to its value? And if so, what do you want to the result of the stringifying? Commented Feb 3, 2016 at 23:08
  • @JohnHascall Thanks for reply, yes I want to find each node "comments" and apply JSON.stringify() to its value, then append the result to the same "comments" node Commented Feb 3, 2016 at 23:10
  • still don't get it, maybe it's the wording you use. can you give an example of input and output? Do you want to serialize sth. or is this your approach to achieve the goal? What do you want to end with? an Object or a String? Commented Feb 3, 2016 at 23:23
  • @Thomas please see my update Commented Feb 3, 2016 at 23:48

1 Answer 1

4

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\":[]}}]}"
        }
    }
]
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for reply, but this seems only work with knowing how many level comments
sorry, I didn't quite get it then. How do you need to have the mechanics working then?
can you show the final JSON that you want to have with already calculated jsonStringify fields please?
the result in your answer is the data format what I I want , but my question is make a function work with don't have to know how many level within the hierarchical data.
oh my god so sorry waste ur time I only read you code but not test, and miss the function calling inside

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.