0

I have to get the values of the following response.

var response =  { "data": [

    {
      "value": "Network problem",
      "text": "Network problem",
      "areas": [
        {
          "value": "Congestion",
          "text": "Congestion",
          "sub_areas": [
            {
              "value": "Congestion_1",
              "text": "Congestion_1",
              "id":"1"
            },
            {
              "value": "Call D",
              "text": "Call D",
              "id":"2"
            }]
        },
        {
          "value": "Drop",
          "text": "Drop",
          "sub_areas": [
            {
              "value": "Drop_1",
              "text": "Drop_1",
              "id":"3"
            }]
        }
      ]};

If I have the value "Network problem", then I have to retrieve its areas(ex: areas = ["Congestion","Drop"]). Then I have to rerieve the sub areas using area value. If area value is "congestion", then sub_areas =["congestion_1", "Call D"]). How can I do it

5

1 Answer 1

1

As suggested by Venky, using a library such as underscore is probably the best way to go.

If you're curious about a native JS solution, I will present one below.

Because your structure does not have many levels of imbrication, you can consider using a simple approach such as:

var node, root = {};

response.data.forEach(function(data) {
  node = root[data.value] = {};

  data.areas.forEach(function(area) {
    node[area.value] = [];

    area.sub_areas.forEach(function(subArea) {
      node[area.value].push(subArea.value);
    });
  });
});

Example output #1: list of 'data' values:

console.log(Object.keys(root));
["Network problem"]

Example output #2: list of areas for 'Network problem':

console.log(Object.keys(root['Network problem']));
["Congestion", "Drop"]

Example output #3: list of sub-areas for 'Network problem' / 'Congestion':

console.log(root['Network problem']['Congestion']);
["Congestion_1", "Call D"]

Note that the root node and its immediate children are objects, while the leaf nodes are arrays. Hence the different syntax for the last example.

Appendix

Below is the response object I used, with fixed formatting.

var response = {
  "data": [
    {
      "value": "Network problem",
      "text": "Network problem",
      "areas": [
        {
          "value": "Congestion",
          "text": "Congestion",
          "sub_areas": [
            {
              "value": "Congestion_1",
              "text": "Congestion_1",
              "id":"1"
            },
            {
              "value": "Call D",
              "text": "Call D",
              "id":"2"
            }
          ]
        },
        {
          "value": "Drop",
          "text": "Drop",
          "sub_areas": [
            {
              "value": "Drop_1",
              "text": "Drop_1",
              "id":"3"
            }
          ]
        }
      ]
    }
  ]
};
Sign up to request clarification or add additional context in comments.

Comments

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.