检查日期已存在的键值对,如果不存在则将其推入数组

时间:2016-12-15 15:00:30

标签: javascript angularjs

我的问题与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);

任何人都可以告诉我我需要做什么。谢谢。

2 个答案:

答案 0 :(得分:0)

您可以先生成所有日期的结果数组,然后重复计算数据。

class SplashWidget(QtGui.QWidget):
    WINDOW_WIDTH= 800
    WINDOW_HEIGHT=600

    WINDOW_TITLE="Splash"
    WINDOW_FOOTER_MESSAGE="Some Text here for [X] Project 2016"
    WINDOW_PARENT=None
    view=None

    def __init__(self, parent=None):
        super(SplashWidget, self).__init__(parent)
        self.WINDOW_PARENT=parent
        self.layout = QtGui.QHBoxLayout()
        self.addWidgets()

        self.view = QWebView(self)
        self.view.setMinimumSize(self.WINDOW_WIDTH,self.WINDOW_HEIGHT-100)
        self.view.setMaximumSize(self.WINDOW_WIDTH,self.WINDOW_HEIGHT-100)
        cwd = os.getcwd()
        self.view.load(QUrl("file://"+cwd+"\\resource\\Splash.html"))
        self.view.show()

        button = MyWidgets.createPushButton("btn",self,100,100,self.complete_name)
        button.move(100,100)
// get the template tasks associated with the event
MATCH (e:MetaEvent {name:"First Day"})<-[:RELEVANT_TO]-(td:ToDo)
// If they have documents, get those too
OPTIONAL MATCH (td)-[docRel:HAS_DOCUMENT]->(d:Document) 
// get the event, and todos, and collect the documents
WITH e, td, collect(d) as documents
// get the event and collect the tasks along with any documents
WITH e, collect ({todo:td, taskDocs:documents}) as tasks
// match the person
MATCH (newHire:Person {name:"John Doe"})-[:HAS_HR_GENERALIST]->(generalist:Person)
// not sure you need this ...
WITH e, newHire, generalist, tasks
// use unwind to loop through all the collected tasks
UNWIND tasks as task
// create a new copy of each task and set any particular properties
CREATE (ntd:ToDo {title:task.todo.title})
SET ntd.event = e.name
// for any document attached to the original template todo, create a similar relationship with the new todo (ntd)
FOREACH(d in task.taskDocs |
    MERGE (ntd)-[:HAS_DOCUMENT]->(d)
)
// Finally ... assign the new todo to the people it needs to be attached to
merge (generalist)<-[:ASSIGNED_BY]-(ntd)-[:ASSIGNED_TO]->(newHire)

答案 1 :(得分:0)

如果允许某些ES6功能,则稍有不同的版本:创建从startDate到endDate的所有日期的映射(包含日期的对象和array2中的所有属性初始化为0)。然后,如果地图中存在name,则循环遍历array2,使用count更新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'];		

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