2016-12-15 45 views
1

我的問題與Check if key value exist and if not push key with value in javascript問題有關。假設我從日曆中選擇日期startDate = 2016-12-10和endDate = 2016-12-16,那麼我會收到來自網絡服務的回覆。檢查鍵值對與日期已存在,如果不存在將其推入到一個數組

array1 = 0:[{name:A,count:2,date:'2016-12-13'},{name:B,count:3,date:'2016-12-13'},{name:C,count:2,date:'2016-12-14'}] 
    1:[{name:A,count:3,date:'2016-12-14'},{name:B,count:3,date:'2016-12-13'},{name:C,count:2,date:'2016-12-12'}] 
    2:[{name:A,count:3,date:'2016-12-11'},{name:B,count:3,date:'2016-12-14'},{name:C,count:2,date:'2016-12-15'},{name:D,count:2,date:'2016-12-13'}]; 

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

我需要做的是通過與名稱作爲名稱值的鍵,總數小時分組。

output = [ 
      {date:2016-12-10, A:0, B:0, C:2, D:0} 
      {date:2016-12-11, A:3, B:0, C:0, D:0}, 
      {date:2016-12-12, A:0, B:0, C:2, D:0}, 
      {date:2016-12-13, A:2, B:6, C:0, D:2}, 
      {date:2016-12-14, A:3, B:3, C:2, D:0}, 
      {date:2016-12-15, A:0, B:0, C:2, D:0}, 
      {date:2016-12-16, A:0, B:0, C:0, D:0} 
     ]; 

我的問題是如何按日期字符串分組。我的代碼看起來像這樣。

var startDate = '2012-12-10'; //input from user 
var enddate = '2012-12-16'; //input from user 

grouped = dateIntervalObj(startDate,endDate,array2); 

array1.forEach(function (a){ 
    a.forEach(function (b){ 
    //@TODO here I have problems 
    grouped[b.hours][b.name] += parseInt(b.cnt); 
    } 
}); 

grouped.sort(function (a, b) { 
    var _a = a.split(':'); 
    _a = parseInt(_a[0])*3600 + parseInt(_a[1])*60 +parseInt(_a[2]); 
    var _b = b.split(':'); 
    _b = parseInt(_b[0])*3600 + parseInt(_b[1])*60 + parseInt(_b[2]); 
    if (_a == _b) return 0; 
    if (_a < _b) return -1; 
    return 1; 
}); 
console.log(grouped); 

任何人都可以請我建議我需要做什麼。謝謝。

+0

'startDate'和'endDate'不匹配'array1'的日期。 –

+0

我想你想改變你的問題的標題爲「檢查與日期的鍵值對已經存在,如果不存在將其推入數組」。除非我不瞭解這個問題。 – MikeSchem

+0

@Scholz可能startDate和endDate之間的每個日期間隔都不能在數組內,但必須在間隔 – Dev

回答

0

您可以先用所有日期生成結果數組,然後迭代數據宿計數。

var array1 = [[{ name: 'A', count: 2, date: '2016-12-13' }, { name: 'B', count: 3, date: '2016-12-13' }, { name: 'C', count: 2, date: '2016-12-14' }], [{ name: 'A', count: 3, date: '2016-12-14' }, { name: 'B', count: 3, date: '2016-12-13' }, { name: 'C', count: 2, date: '2016-12-12' }], [{ name: 'A', count: 3, date: '2016-12-11' }, { name: 'B', count: 3, date: '2016-12-14' }, { name: 'C', count: 2, date: '2016-12-15' }, { name: 'D', count: 2, date: '2016-12-13' }]], 
 
    array2 = ['A', 'B', 'C', 'D'], 
 
    startDate = '2016-12-10', 
 
    endDate = '2016-12-16', 
 
    result = function (data, count, start, end) { 
 
     function insertObject() { 
 
      var o = { date: dateISO }; 
 

 
      array2.forEach(function (k) { o[k] = 0; }); 
 
      hash[dateISO] = o; 
 
      r.push(hash[dateISO]); 
 
     } 
 

 
     var date = new Date(start), 
 
      dateISO = date.toISOString().slice(0, 10), 
 
      hash = Object.create(null), 
 
      r = []; 
 

 
     insertObject(); 
 
     while (dateISO !== end) { 
 
      date.setDate(date.getDate() + 1); 
 
      dateISO = date.toISOString().slice(0, 10); 
 
      insertObject(); 
 
     } 
 

 
     array1.forEach(function (a) { 
 
      a.forEach(function (b) { 
 
       if (hash[b.date]) { 
 
        hash[b.date][b.name] += b.count; 
 
       } 
 
      }); 
 
     }); 
 
     return r; 
 
    }(array1, array2, startDate, endDate); 
 

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

0

稍微不同的版本,如果一些ES6特徵被允許:創建地圖上的所有日期從STARTDATE至結束日期的(與含有日期對象和從數組2的所有屬性初始化爲0) 。然後遍歷數組2與count更新name如果存在於地圖date

const array1 = [{name:'A',count:2,date:'2016-12-13'},{name:'B',count:3,date:'2016-12-13'}, {name:'C',count:2,date:'2016-12-14'},{name:'A',count:3,date:'2016-12-14'},{name:'B',count:3,date:'2016-12-13'},{name:'C',count:2,date:'2016-12-12'},{name:'A',count:3,date:'2016-12-11'},{name:'B',count:3,date:'2016-12-14'},{name:'C',count:2,date:'2016-12-15'},{name:'D',count:2,date:'2016-12-13'},{name:'D',count:200,date:'2016-12-09'}], 
 
    array2 = ['A','B','C','D']; \t \t 
 

 
function getDates(startDate, endDate){ 
 
    let dt = new Date(startDate), 
 
     dte = new Date(endDate), 
 
     map = new Map(); 
 
    while(dt <= dte){  \t 
 
     let obj = array2.reduce(function(o, name){o[name] = 0; return o; }, {date:dt.toISOString().split('T')[0]}); //create empty object 
 
     map.set(obj.date, obj); //store in map with date as the key for quick referencing later  
 
     dt.setDate(dt.getDate()+1); //increase date with 1 day 
 
    } 
 

 
    for(let o of array1) 
 
     if(map.has(o.date)) //if the date exists in map = day is in range 
 
     \t map.get(o.date)[o.name] += o.count; 
 
     
 
    return [...map.values()]; //return the values of the map as an array 
 
} 
 

 
console.log(getDates('2016-12-10', '2016-12-16')); //example in OP used 2012, assuming 2016

相關問題