使用带有JavaScript的hashmap计算重复的JSON数组值

问题描述 投票:3回答:2

我无法将注意力集中在javascript哈希图和JSON数组上。我正在尝试从JSON数组计算重复的导入国家/地区。例如,数据为:

[
  {
    "id": 1,
    "import_country": "Russia",
    "model": "Express 2500",
    "make": "Chevrolet",
    "sold_by": "Sibylla Kneale",
    "sale_price": 14702
  },
  {
    "id": 2,
    "import_country": "Philippines",
    "model": "G-Class",
    "make": "Mercedes-Benz",
    "sold_by": "Gabie Gradwell",
    "sale_price": 19142
  },
  {
    "id": 3,
    "import_country": "Russia",
    "model": "M",
    "make": "Infiniti",
    "sold_by": "Burl Pitkeathley",
    "sale_price": 18395
  }
]

这是到目前为止我拥有的代码:

var country = [];
var temp = [];
const model = [];

const count = 0;
const response = await fetch('some api url where data is retrieved')
    .then(response => response.json())
    .then(data => {
        var key = {};
        for(i = 0; i< data.length; i++){
            if(temp.indexOf(data[i].import_country) == -1){
                temp.push(data[i][import_country]);
                var _data = {};

            }
        }
    });

我的最终目标是在图表上显示国家总数。

javascript json multidimensional-array
2个回答
2
投票

尝试下面的代码-

var jsonArray = [{"id":1,"import_country":"Russia","model":"Express 2500","make":"Chevrolet","sold_by":"Sibylla Kneale","sale_price":14702},{"id":2,"import_country":"Philippines","model":"G-Class","make":"Mercedes-Benz","sold_by":"Gabie Gradwell","sale_price":19142},{"id":3,"import_country":"Russia","model":"M","make":"Infiniti","sold_by":"Burl Pitkeathley","sale_price":18395}];
var map = new Map();
for(var i=0;i<jsonArray.length;i++){
    if(!map.get(jsonArray[i].import_country)){
        map.set(jsonArray[i].import_country,1);
    }else{
        map.set(jsonArray[i].import_country, map.get(jsonArray[i].import_country) + 1);
    }
}

伪代码:-

  1. 获取JSON数组。
  2. 创建空白地图。
  3. for i = 0到JSON数组长度。

    a。如果地图不包含国家/地区,则将国家/地区设置为1。

    b。否则通过增加计数1来设置地图中已经存在的键。

  4. 结束。

2
投票

执行此操作的一种好方法是使用哈希图而不是您所说的数组。

如果我们将您的代码更新为:

var hashmap = {};

const response = await fetch('some api url where data is retrieved')
  .then(response => response.json())
  .then(data => {
    var key = {};
    for(i = 0; i< data.length; i++){
      let country = data[i]['import_country']; 
      if (hashmap[country] == void(0)) { // if it doesn't exist in the hashmap
        hashmap[country] = []; // create it in the map
      }
      hashmap[country].push(data[i]); // add it to the country array in the temp
    }
  }) 

如果上面接收的数据正确,则输出将类似于以下内容:

{
  "Russia": [
    {"id":1,"import_country":"Russia","model":"Express 2500","make":"Chevrolet","sold_by":"Sibylla Kneale","sale_price":14702},
    {"id":3,"import_country":"Russia","model":"M","make":"Infiniti","sold_by":"Burl Pitkeathley","sale_price":18395}
  ],
  "Phillipines": [
    {"id":2,"import_country":"Philippines","model":"G-Class","make":"Mercedes-Benz","sold_by":"Gabie Gradwell","sale_price":19142}
  ]
}

现在我们已经将数据格式化为想要的格式,我们可以遍历该数据以获取国家总数:

... // code from above
for (var country in map) {
   console.log(country + ": " + country.length + " cars");
}

该代码将输出:

Russia: 2 cars
Phillipines: 1 cars
© www.soinside.com 2019 - 2024. All rights reserved.