JavaScript Creating Array with String and Integers from JSON creating array with for loop python,creating array with values

Currently I've got the following JSON feed:

var data = {
  "feeds": {
    "regions": [{
        "name": "Lichtenberg",
        "id": "01408.b",
        "suburbs": [{
            "name": "Fennpfuhl",
            "views": 76400
          },
          {
            "name": "Lichtenberg",
            "views": 87895
          },
          {
            "name": "Rummelsberg",
            "views": 10239
          }
        ]
      },
      {
        "name": "Mitte",
        "id": "03442.f",
        "suburbs": [{
            "name": "Tiergarten",
            "views": 82695
          },
          {
            "name": "Mitte",
            "views": 67234
          },
          {
            "name": "Hansaviertel",
            "views": 10848
          },
          {
            "name": "Moabit",
            "views": 67500
          }
        ]
      },
      {
        "name": "Friedrichshain-Kreuzberg",
        "id": "01991.o",
        "suburbs": [{
            "name": "Friedrichshain",
            "views": "98494"
          },
          {
            "name": "Kreuzberg",
            "views": "27800"
          }
        ]
      },
      {
        "name": "Templehof-Schöneberg",
        "id": "01778.k",
        "suburbs": [{
            "name": "Friedenau",
            "views": 76595
          },
          {
            "name": "Schöneberg",
            "views": 20731
          },
          {
            "name": "Templehof",
            "views": 58000
          },
          {
            "name": "Mariendorf",
            "views": 32300
          }
        ]
      },
      {
        "name": "Pankow",
        "id": "02761.q",
        "suburbs": [{
            "name": "Wießensee",
            "views": 81294
          },
          {
            "name": "Prenzlauer Berg",
            "views": 76470
          },
          {
            "name": "Pankow",
            "views": 90210
          }
        ]
      }
    ],
  }
};

Effectively I want to do two things:

  1. Loop through the Regions to get the 4 names
  2. Loop through all the views in each region, sum them up, and return them as values under the 4 names.

Here's a sample of the output that I just quickly typed up:

var viewsPerRegion = 

 [{
  label: "Litchtenberg",
  total: 174534
}, {
  label: "Mitte",
  total: 228277
}, {
  label: "Friedrichshain-Kreuzberg",
  total:  126294
}, {
  label: "Templehof-Schöneberg",
  total: 187626
}]; 
etc...

I do want to note that data.feeds.region[2].suburbs.views is stored as a string, so that's something I'll need to change into an integer first.

Anyway the solution I have so far (which doesn't really work) is as follows:

var viewsPerRegion, i, j, x;  

for (i in data.feeds.regions) {
  x += data.feeds.regions[i].name;
  for (j in data.feeds.regions[i].suburbs.views){
    x += data.feeds.regions[i].suburbs.views[j];
  }
}

viewsPerRegion = x;

Any help is certainly appreciated - bit of a newbie in JSON and javascript.

Answer:1

I need a textarea to include a set of double quotes at the start and end of the textarea value. The below code works in that double quotes are added to the start and end of the field, but if the user ...

I need a textarea to include a set of double quotes at the start and end of the textarea value. The below code works in that double quotes are added to the start and end of the field, but if the user ...

I have an URL looking like this: https://www.website.com/dk/da/home/category/ I am trying to remove the last forward slash and the text before it, untill it reaches the new forwardslash. Meaning i ...

I have an URL looking like this: https://www.website.com/dk/da/home/category/ I am trying to remove the last forward slash and the text before it, untill it reaches the new forwardslash. Meaning i ...

  1. remove last forward slash javascript
  2. remove last forward slash c#
  3. php remove last forward slash
  4. bash remove last forward slash from string
  5. regex replace last forward slash

I have a function to call API from the server like this: getDataSet(callback) { request.open('POST', `${apiPath}`); request.setRequestHeader('Content-Type', 'application/x-...

I have a function to call API from the server like this: getDataSet(callback) { request.open('POST', `${apiPath}`); request.setRequestHeader('Content-Type', 'application/x-...

  1. json data response example
  2. json data response
  3. json data response ajax
  4. json data response jquery
  5. json response data format
  6. json response data type
  7. json response data count
  8. json response data undefined
  9. json response data python
  10. json response data model
  11. get json data response
  12. sample json data response
  13. send json data response
  14. json binary data response
  15. jbuilder json_data_response
  16. c# json data response
  17. access json response data
  18. axios json response data
  19. json.parse(response.data)
  20. laravel json response data

I have a JSON structure like this: [ {"menuId":"1001","depth":"1","parentId":"0"}, {"menuId":"1002","depth":"1","parentId":"0"}, {"menuId":"1003","depth":"2","parentId":"1001"}, {"...

I have a JSON structure like this: [ {"menuId":"1001","depth":"1","parentId":"0"}, {"menuId":"1002","depth":"1","parentId":"0"}, {"menuId":"1003","depth":"2","parentId":"1001"}, {"...