【问题标题】:Calculate population percentage in javascript calling two different json files在调用两个不同的json文件的javascript中计算人口百分比
【发布时间】:2020-04-09 01:43:32
【问题描述】:

我正在尝试使用 Javascript 计算美国每个县的亚裔人口百分比。

为此,我从 Census Api 调用数据。这是我目前写的代码。


  let totalPopulation = await d3.json("https://api.census.gov/data/2018/acs/acs5?get=B03002_001E&for=county:*");
  let asianPopulation = await d3.json("https://api.census.gov/data/2018/acs/acs5?get=B03002_004E&for=county:*");

  let myMap = new Map();

  Object.keys(totalPopulation).forEach((key) => myMap.set(key, asianPopulation * 100 / totalPopulation));

  return myMap;

我知道这是错误的,但考虑到数据是以这种方式格式化的,我不知道如何使它工作:

[["B03002_004E","state","county"],
["282","28","151"],
["17","28","111"],
["15","28","019"],
["57","28","057"],
["0","28","015"],
["0","28","043"],
["33","28","063"],

理想情况下,我想获得一个包含“州”+“县”变量和百分比变量的对象数组。如果可以保持总人口和亚洲人口,那也很棒。

关于如何正确实现这一点的任何想法?

【问题讨论】:

    标签: javascript lodash census


    【解决方案1】:

    我认为对 totalPopulation 和 asianPopulation 进行简单的 for 循环可能会产生理想的结果。

    var percentageAsianPopulationByCounty = [];
    
    for( var i=1; i < asianPopulation.length; i++)
    {
      percentageAsianPopulationByCounty.push(
              new Array((asianPopulation[i][0]/totalPopulation[i][0])*100,   asianPopulation[i][1], asianPopulation[i][2] )
    )
    }
    

    【讨论】:

    • 这很好用。我如何映射 percentAsianPopulationByCounty 以获得这样的结构 {"10001" => 63, "10003" => 32, "10005" => 12, ... } 谢谢线索位!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 1970-01-01
    相关资源
    最近更新 更多