Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从对象数组中获取唯一值_Javascript - Fatal编程技术网

Javascript 从对象数组中获取唯一值

Javascript 从对象数组中获取唯一值,javascript,Javascript,我有以下代码: fetchDemo(carMakesUrl).then(function(result) { for(var i = 0; i < result.length; i++){ for(var prop in result[i]){ if(prop.length > 1){ console.log(result[i

我有以下代码:

        fetchDemo(carMakesUrl).then(function(result) {
            for(var i = 0; i < result.length; i++){
                for(var prop in result[i]){
                    if(prop.length > 1){
                        console.log(result[i]['make_country']);
                    }
                }
            }
        });
我要找的是:

意大利重复了17次,这意味着“它生产17种类型的汽车”,因此其他国家将生产许多不同的汽车

如何使console.log只输出一次国家名称及其生产的汽车数量

fetchDemo(carMakesUrl).then(function(result) {    
  var obj = {};
  result.forEach(function(ele,ind){obj[ele.make_country] = (obj[ele.make_country] || 0) + 1});
  console.log(obj);
});
你可以这样计算


您可以这样计算。

我假设obj=结果。希望这对你有用。和country\u car\u计数器对象保存所需的输出

var country_car_counter = {};
      obj.map(function(item){
        var property = item.make_country;

         country_car_counter[property] = (country_car_counter[property])?country_car_counter[property] + 1 : 1 ;

      })
      console.log('this is the required object',country_car_counter)

我假设obj=结果。希望这对你有用。和country\u car\u计数器对象保存所需的输出

var country_car_counter = {};
      obj.map(function(item){
        var property = item.make_country;

         country_car_counter[property] = (country_car_counter[property])?country_car_counter[property] + 1 : 1 ;

      })
      console.log('this is the required object',country_car_counter)

可能的重复项您是否尝试创建一个以国家名称和生产的汽车为参数的函数,然后在每个for循环结束时调用它?@Alex重复标记的问题正是您要查找的问题。可能的重复项您是否尝试创建一个以国家名称和生产的汽车为参数的函数,然后在每个for循环结束时调用它?@Alex重复标记的问题正是您要寻找的。