检查密钥值是否存在,如果没有,则在javascript中按值键 [英] Check if key value exist and if not push key with value in javascript

查看:44
本文介绍了检查密钥值是否存在,如果没有,则在javascript中按值键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的网络服务有这样的响应数组

Suppose I have response array from my web-service like this

Array1 = [
      0:[{name:A,count:2,hours:3},{name:B,count:3,hours:3},{name:C,count:2,hours:4}]
      1:[{name:A,count:3,hours:4},{name:B,count:3,hours:3},{name:C,count:2,hours:2}]
      2:[{name:A,count:3,hours:1},{name:B,count:3,hours:4},{name:C,count:2,hours:5},{name:D,count:2,hours:3}]
    ];

Array2 = ['A','B','C','D'];

在我的输出中,我需要检查24小时。但为简单起见,我们现在只采取小时到5.来自Array1的每个子数组属于一个用户。现在,我必须按小时和活动对数组进行分组。意味着我需要将密钥作为名称和值作为每小时的名称总数。

In my output, I need to check for 24 hours.But for simplicity,we will now take only hours to 5.Each sub array from Array1 belong to one user. Now,I must group array by hours and activity. Means I need to make key as name and value as total count of name in each hours.

输出必须如下所示

var output = [
               {hours:1, A:3, B:0, C:0, D:0},
               {hours:2, A:0, B:0, C:2, D:0},
               {hours:3, A:2, B:6, C:0, D:2},
               {hours:4, A:3, B:3, C:2, D:0},
               {hours:5, A:0, B:0, C:2, D:0},
             ];

我在下面尝试

angular.forEach(Array1 , function(array){ //angularjs foreach
    array.forEach(function(a){
      obj[a.hours] = obj[a.hours]||[0];
      if(obj[a.hours].hasOwnProperty(a.name)){
        obj[a.hours][a.name] = parseInt(obj[a.hours][a.name]) + parseInt(a.count);
      }else{
        obj[a.hours][a.name] = parseInt(a.count);
      }
      obj[a.hours]['hours'] = a.hours;
    });
  });

我尝试将我的数组分组为小时,名称为密钥,总计数为值。我尝试的更多是

where I try to group my array with hours and name as key and total count as value. What more I try is

 var startHour = 1;
 var endHours = 5;
 var newData = [];  //@TODO
 newData.push(obj); //@TODO
 for(i= startDate; i < endDate; i++) {
    var found = newData.some(function(el){
      //el.forEach(function(a){
        $.each(el, function(key, value) {
          if(value.hours){
            return value.hours === i;
          }
      });
    });
    if(!found){
      console.log(i + "not found");
      newData.push([{'hours':i}]);
    }
  }
  console.log(newData);

但是每次我都找不到。作为我的输出我需要按键值对名称,如果不退出则计算0。但首先我尝试只推出几小时(如果不存在)。任何人都可以建议我做错了什么。我是后端程序员所以,我对JavaScript没有很好的了解。

But every time I am in not found.As my output I need to push key-value pairs name and count 0 if not exit. But first I try to push only hours if not exists. Can anyone suggest me what I did wrong. I am back-end programmer so,I don't have good knowledge of JavaScript.

谢谢你。

推荐答案

您可以使用哈希表来引用正确的小时对象,并使用 Array#forEach 。稍后对所需订单的结果数组进行排序。

You could use a hash table for the reference of the right hour object and iterate the data with Array#forEach. Later sort the result array for the wanted order.

var array1 = [[{ name: 'A', count: 2, hours: 3 }, { name: 'B', count: 3, hours: 3 }, { name: 'C', count: 2, hours: 4 }], [{ name: 'A', count: 3, hours: 4 }, { name: 'B', count: 3, hours: 3 }, { name: 'C', count: 2, hours: 2 }], [{ name: 'A', count: 3, hours: 1 }, { name: 'B', count: 3, hours: 4 }, { name: 'C', count: 2, hours: 5 }, { name: 'D', count: 2, hours: 3 }]],
    array2 = ['A', 'B', 'C', 'D'],
    grouped = [];

array1.forEach(function (a) {
    a.forEach(function (b) {
        if (!this[b.hours]) {
            this[b.hours] = { hours: b.hours };
            array2.forEach(function (c) { this[c] = 0; }, this[b.hours]);
            grouped.push(this[b.hours]);
        }
        this[b.hours][b.name] += b.count;
    }, this);
}, Object.create(null));

grouped.sort(function (a, b) { return a.hours - b.hours; });

console.log(grouped);

.as-console-wrapper { max-height: 100% !important; top: 0; }

24小时数组的提案,零基础小时

Proposal with 24h array, with zero based hour.

var array1 = [[{ name: 'A', count: 2, hours: 3 }, { name: 'B', count: 3, hours: 3 }, { name: 'C', count: 2, hours: 4 }], [{ name: 'A', count: 3, hours: 4 }, { name: 'B', count: 3, hours: 3 }, { name: 'C', count: 2, hours: 2 }], [{ name: 'A', count: 3, hours: 1 }, { name: 'B', count: 3, hours: 4 }, { name: 'C', count: 2, hours: 5 }, { name: 'D', count: 2, hours: 3 }]],
    array2 = ['A', 'B', 'C', 'D'],
    grouped = Array.apply(null, { length: 24 }).map(function (_, i) {
        var o = { hours: i }
        array2.forEach(function (a) { this[a] = 0; }, o);
        return o;
    });

array1.forEach(function (a) {
    a.forEach(function (b) {
        grouped[b.hours][b.name] += b.count;
    }, this);
}, Object.create(null));

console.log(grouped);

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于检查密钥值是否存在,如果没有,则在javascript中按值键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆