循环遍历数组列表以计算对象

时间:2015-10-07 12:40:09

标签: javascript arrays json loops if-statement

我在使用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>

我希望将对象的数量推入空数组&#34; counter&#34;当你控制日志&#34; counter&#34;例如

网关
        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
       ir_yes 13        ir_no 18

制造商
       homematic 24        fs20 59        intertechno 38        埃罗63        homeeasy 40        somfy 11

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

3 个答案:

答案 0 :(得分:0)

改变这个:

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

对此:

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

答案 1 :(得分:0)

希望这段代码有用。

&#13;
&#13;
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));
&#13;
&#13;
&#13;

获得的输出:

{
    "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
    }
}

答案 2 :(得分:0)

谢谢你们,但我坐下来以为我得到了我所缺少的东西;

        //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);