【问题标题】:Looping through a list of arrays to count the objects循环遍历数组列表以计算对象
【发布时间】:2015-10-07 12:40:08
【问题描述】:

我在用 javascript 计算这个数组中的对象数量时遇到了麻烦。 下面是我尝试用我的代码计算的对象数组。

       <script>

var arr = [
    {"gateways":["ccu1"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_cam","ip_other"]},
    {"gateways":["v3"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_cam"]},
    {"gateways":["v2","v3","v4","ccu2"],"manufacturer":["homematic","intertechno"],"ir":["ir_yes"],"ip":["ip_cam","ip_other"]},
    {"gateways":["v2","ccu1","ccu2"],"manufacturer":["homematic"],"ir":["ir_yes"],"ip":["ip_cam","ip_other"]},
    {"gateways":["gw_none"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_cam"]},
    {"gateways":["v3","ccu2"],"manufacturer":["homematic","fs20","intertechno","elro","Eltako Enocean"],"ir":["ir_yes"],"ip":["ip_cam","ip_other"]},
    {"gateways":["v3","v4"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_other"]},
    {"gateways":["v3","v4"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_other"]},
    {"gateways":["v2"],"manufacturer":["intertechno"],"ir":["ir_yes"],"ip":["ip_other"]}
];
var counter = [];
for(var i=0; i<arr.length; i++) {
    //console.log(arr[i]);

    for(var index in arr[i]) {
        console.log(index);

        if(counter[index] === undefined) {
            counter[index] = [];
        }

    }
}
console.log(counter);
    </script>

我希望在控制台记录“计数器”时将对象的数量推入空数组“计数器”,例如

网关
ccu2 42 v4 70 v2 95 v3 91 v4plus 32 ccu1 16 gw_none 10

IP
ip_cam 4 ip_other 10 ip_none 4

他们
ir_yes 13 ir_no 18

制造商
家庭式 24 fs20 59 国际技术 38 埃尔罗 63 家常 40 苏菲 11

我是编程新手,我尝试过一些类似这样的练习,但我被卡住了。我留下了将对象计数器放入空数组的代码。我试过但不能让它工作。我会很感激任何帮助,我希望我的任务是有意义的并且是可以理解的。

【问题讨论】:

  • 数组中没有arr.length对象吗?
  • 首先将counter 初始化为一个对象,而不是一个数组,即var counter = {};。数组使用数字键,而您想使用字符串作为键。 jsfiddle.net/frsbawwz

标签: javascript arrays json loops if-statement


【解决方案1】:

改变这个:

 if(counter[index] === undefined) {
        counter[index] = [];
 }

到这里:

 if(counter[index] === undefined) {
        counter[index] = [];
 }
 counter[index].push( arr[i][index] );  

【讨论】:

    【解决方案2】:

    希望此代码对您有所帮助。

    var arr = [
        {"gateways":["ccu1"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_cam","ip_other"]},
        {"gateways":["v3"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_cam"]},
        {"gateways":["v2","v3","v4","ccu2"],"manufacturer":["homematic","intertechno"],"ir":["ir_yes"],"ip":["ip_cam","ip_other"]},
        {"gateways":["v2","ccu1","ccu2"],"manufacturer":["homematic"],"ir":["ir_yes"],"ip":["ip_cam","ip_other"]},
        {"gateways":["gw_none"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_cam"]},
        {"gateways":["v3","ccu2"],"manufacturer":["homematic","fs20","intertechno","elro","Eltako Enocean"],"ir":["ir_yes"],"ip":["ip_cam","ip_other"]},
        {"gateways":["v3","v4"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_other"]},
        {"gateways":["v3","v4"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_other"]},
        {"gateways":["v2"],"manufacturer":["intertechno"],"ir":["ir_yes"],"ip":["ip_other"]}
    ];
    
    
    var types = Object.keys(arr[0]); //Returns ["gateways","manufacturer","ir","ip"]
    var counter = {};
    types.forEach(function(type){
      var values = [].concat.apply([], arr.map(function(d){ return d[type] })); // Find all values for each key like gateways
      //Count occurrence of each value
      var counts = {};  
      for(var i = 0; i< values.length; i++) {
        var num = values[i];
        counts[num] = counts[num] ? counts[num]+1 : 1;
      }
      counter[type] = counts;
    });
    
    alert(JSON.stringify(counter));

    获得的输出:

    {
        "gateways": {
            "ccu1": 2,
            "v3": 5,
            "v2": 3,
            "v4": 3,
            "ccu2": 3,
            "gw_none": 1
        },
        "manufacturer": {
            "homematic": 8,
            "intertechno": 3,
            "fs20": 1,
            "elro": 1,
            "Eltako Enocean": 1
        },
        "ir": {
            "ir_no": 5,
            "ir_yes": 4
        },
        "ip": {
            "ip_cam": 6,
            "ip_other": 7
        }
    }
    

    【讨论】:

      【解决方案3】:

      谢谢大家,但我坐下来认为我得到了我所缺少的东西;

              //first we initialised counter 
          var counter = [];
      
          //we then loop over the big array
          for(var i=0; i<arr.length; i++) {
      
              //we save then the single objects
              var obj = arr[i];
      
              // We then evaluate Object -> looping and count on each entry
              for(var key in obj) {
      
              //check whether there is already an entry for the respective
              //index (gateways, Manufacturer etc)
                  if(counter[key] === undefined) {
                      counter[key] = [];
                  }
      
      
                  //Save the individual array of Object entries
                  var arr2 = obj[key];
      
                  //Looping and counting the array
                  for(var k=0; k<arr2.length; k++) {                    
                      var entry = arr2[k];
      
                      //Check whether there is already a counter for that
                      //item
                      if(counter[key][entry] === undefined) {
                          counter[key][entry] = 1;
                      } else {
                          counter[key][entry]++;
                      }
                  }
              }
          }
          console.log(counter);
      

      【讨论】:

        猜你喜欢
        • 2021-03-24
        • 1970-01-01
        • 2021-10-26
        • 1970-01-01
        • 1970-01-01
        • 2016-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多