检查键值是否存在,如果不在JavaScript中的值推动键



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

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

和输出看起来像这样

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

谢谢。

您可以使用哈希表作为正确的小时对象的引用,并用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个对象)开始。并将其推入此数组中的新的附加计数,以相对应的时间和名称:

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

最新更新