检查密钥值是否存在,如果没有,则在javascript中按值键

时间:2016-12-14 13:52:56

标签: javascript angularjs arrays

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

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的每个子数组都属于一个用户。现在,我必须按小时和活动对数组进行分组。意味着我需要将密钥作为名称和值作为每小时的名称总数。

输出必须如下所示

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

我尝试将我的数组分组为小时,名称为键,总计数为值。我还尝试了什么

 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没有很好的了解。

谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用哈希表作为右小时对象的引用,并使用Array#forEach迭代数据。稍后对所需订单的结果数组进行排序。

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小时数组,基于零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; }

答案 1 :(得分:0)

你可以从一个对象数组开始(每小时1个对象)。在这个数组中推进新的附加计数,对应于小时和名称:

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}]];
var Array2 = ['A','B','C','D'];
var res = [1,2,3,4,5].map(x => ({"hours": x})).map(x => {
  Array2.forEach(y => x[y] = 0);
  return x;
});
Array1.reduce((a,b) => a.concat(b)).forEach(x => {
  if (Array2.indexOf(x.name) !== -1)
    res[x.hours - 1][x.name] += x.count;
})
console.log(res);