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

使用带有javascript的hashmap计算重复JSON数组值,javascript,json,multidimensional-array,Javascript,Json,Multidimensional Array,我很难将注意力集中在javascript哈希映射和JSON数组上。我试图从JSON数组中计算重复的进口国。例如,数据是: [ { "id": 1, "import_country": "Russia", "model": "Express 2500", "make": "Chevrolet", "sold_by": "Sibylla Kneale", "sale_price": 14702 }, { "id": 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 = {};

            }
        }
    });
var国家=[];
var-temp=[];
常数模型=[];
常数计数=0;
const response=wait fetch('检索数据的某个api url')
.then(response=>response.json())
。然后(数据=>{
var key={};
对于(i=0;i

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

请尝试下面的代码-

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);
    }
}
var jsonArray=[{“id”:1,“进口国”:“俄罗斯”,“车型”:“Express 2500”,“制造”:“雪佛兰”,“销售人”:“Sibylla Kneale”,“销售价格”:14702},{“id”:2,“进口国”:“菲律宾”,“车型”:“G级”,“制造”:“梅赛德斯-奔驰”,“销售人”:“Gabie Gradwell”,“销售价格”:19142},{“id”:3,“进口国”:“俄罗斯”,“车型”:“M”,“制造”:“英菲尼迪”,“销售人”:“Burl Pitkeathley”,“售价:18395}];
var map=newmap();

对于(var i=0;i来说,一个好方法是使用hashmap,而不是像您所说的数组

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

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
    }
  }) 
现在,我们已经按照我们希望的方式对数据进行了格式化,我们可以循环使用它来获得国家总数:

... // code from above
for (var country in map) {
   console.log(country + ": " + country.length + " cars");
}
该代码将输出:

Russia: 2 cars
Phillipines: 1 cars

你能添加预期的结果吗?增加国家数的代码在哪里?很好的一点@AmanKumayu-我解释为获得每个国家的汽车总数,而不仅仅是国家总数。谢谢!我从API中看到每个国家都有正确的键值对。更简短的代码:
countries.map(v=>v.import\u country).filter((v,i,a)=>a.indexOf(v)!==i.length);
Russia: 2 cars
Phillipines: 1 cars