简体   繁体   中英

Check key value pairs with date already exist and if NOT exists push it into an array

My problems is related to Check if key value exist and if not push key with value in javascript issues. Suppose I have a response from web-service if I select date from calendar startDate = 2016-12-10 and 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'}];

and

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

What I need to do is grouped by hours with name as key and total count of name as value.

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

My problem is how to grouped by date string.MY code look like this.

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

Can anyone please suggest me what I need to do.Thank You.

You could generate the result array first with all dates and the iterate the data dor counting.

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

A slightly different version if some ES6 features are allowed: create a map of all the dates from startDate to endDate (with objects containing the dates and all properties from array2 initialized as 0). Then loop through array2 to update name with count if the date exists in the map:

 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']; function getDates(startDate, endDate){ let dt = new Date(startDate), dte = new Date(endDate), map = new Map(); while(dt <= dte){ 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 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM