从JSON使用字符串和整数创建数组

时间:2018-08-30 03:28:49

标签: javascript json

目前,我有以下JSON提要:

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
          }
        ]
      }
    ],
  }
};

有效地,我想做两件事:

  1. 遍历各地区以获得4个名字
  2. 遍历每个区域中的所有视图,对其进行汇总,然后将其作为4个名称下的值返回。

以下是我刚刚快速输入的输出示例:

var viewsPerRegion = 

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

我确实要注意data.feeds.region [2] .suburbs.views是作为字符串存储的,因此我需要首先将其更改为整数。

无论如何,到目前为止,我有(实际上没有用的)解决方案如下:

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;

当然可以提供任何帮助-JSON和javascript中的新手。

1 个答案:

答案 0 :(得分:6)

您可以map个区域数组,从每个区域中提取name,并通过使用total将每个reduce加起来得到views。 :

const 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}]}],}}

const viewsPerRegion = data.feeds.regions.map(({ name, suburbs }) => ({
  label: name,
  total: suburbs.reduce((a, { views }) => a + Number(views), 0)
}));
console.log(viewsPerRegion);