复杂的日期数组操作

Complicated array manipulation with dates

本文关键字:操作 数组 日期 复杂      更新时间:2023-09-26

我正在为一个不接受开箱即用行为的客户端构建一个类似于自定义日历的东西,并且我在导航如何拼凑所需的数组以正确显示日期时遇到了一些问题。

一些背景:这是一个基于SharePoint的解决方案,用户在一个列表上输入旅行日期和地点,在另一个列表中输入默认位置。输出需要是一个四周的时间跨度,显示用户在任何一天的位置,无论是这些旅行地点还是默认的(家庭办公室)地点。

因此,我对旅行列表([行踪])和默认位置列表([defaults])进行REST调用,并在代码中做一些其他事情来构建一个从今天到今天+28的日期数组([allDates])。

我已经尝试了一堆嵌套的for循环,但它并没有真正返回正确的东西。但我需要基本上说:在[所有日期]中的每一天,[行踪]中是否有该日期的事件?如果是,则按下事件标题。如果没有,则从[默认值]推送默认位置。

我在这里完全迷路了,伙计们,任何帮助都非常感谢。

编辑

作为对Rajesh的回答,[行踪]是一个具有以下属性的Events对象数组:

  • 日期(UTC字符串)
  • 事件长度(字符串)
  • 团队成员(字符串)
  • 标题(字符串)

[allDates]是从今天到今天+28天的日期字符串数组。

[默认值]是具有以下属性的对象数组:

  • 团队成员(字符串)
  • 位置(字符串)

还有一个我正在尝试的例子:

(完成REST调用并返回数据后)

function initCal(){
    //Groups returned whereabouts data into arrays per team member name.
    var grouped = _(whereabouts).groupBy(function(o){
        return o.Team_x0020_Member.Title;
    });
    //Reduces the above to something a little more simple for me to work with.
    grouped = _.pairs(grouped)          
    //Full disclosure, this is going into a google charts data table.
    var tbl = new google.visualization.DataTable();
    //Building the allDates array and adding in day names.  
    for(var i=0;i<14;i++){
        var weekday = new Array(7);
        weekday[0]=  "Sunday";
        weekday[1] = "Monday";
        weekday[2] = "Tuesday";
        weekday[3] = "Wednesday";
        weekday[4] = "Thursday";
        weekday[5] = "Friday";
        weekday[6] = "Saturday";                
        var day = today.addDays(i);
        var d = weekday[day.getDay()];
        var n = day.toLocaleDateString();
        tbl.addColumn('string',n);
    }
    //I have to do some manipulation of the object google creates.  It looks kind of weird, but the output is that allDates array.
    var allDates = [];
    var colsArr = tbl.Kf;               
    for(var i=0;i<colsArr.length;i++){
        if(colsArr[i].label != ''){
            allDates.push(colsArr[i].label);
        }
    }
    //This is ultimately where the results will go.
    var rows = [];
    //Cycle through grouped, get the current team member, then go manipulate all these arrays using getEvents. 
    for(var i=0;i<grouped.length;i++){
        var thisTM = grouped[i][0];
        var whereabouts = grouped[i][1];
        rows.push([thisTM,getEvents(whereabouts,defaults,allDates)]);
    }
    //This is just where the data table goes.       
    var table = new google.visualization.Table(document.getElementById('whereabouts'));
            table.draw(tbl, {width: '100%', height: '100%'});           
}
//Just needed something to get the day names.  Not important, left here because laziness.
Date.prototype.addDays = function(days){
    var dat = new Date(this.valueOf());
    dat.setDate(dat.getDate() + days);
    return dat;
}

function getEvents(whereabouts,defaults,allDates){
    //Figure I'll put the finished results here and return this array.
    var events = [];
    //Empty array for formatted dates.
    var evDate = [];
   //Format the whereabouts date strings into locale date strings so I can do comparisons against the allDates date strings.
   for(var i=0;i<whereabouts.length;i++){
       var formatD = new Date(whereabouts[i].Date).toLocaleDateString();
          evDate.push(formatD);
   }
   for(var i=0;i<allDates.length;i++){
       for(var j=0;j<evDate.length;j++){
          if (evDate[j] === allDates[i]){
              events.push(whereabouts[j].Title);
          } else {
              events.push(defaults.Title);
          }
      } 
   }    
}

编辑2还有一段上下文。应该返回给initCal的应该是一个与allDays长度完全相同的数组,但包含事件标题(如果有)或默认位置(如果当天没有事件)。

这有道理吗?再次感谢您的帮助和反馈。

不是一个真正的答案,但注释还不够大。

据推测,"UTC字符串"是指ISO 8601扩展格式的日期,如"2016-03-29T10:43:23Z"或类似的日期。大多数浏览器都会正确地解析它,但我建议你自己解析(这里有很多问题)。

对于比较,如果您只想匹配日期(没有时间),则可以比较格式相同的字符串,也可以只获取时间值并进行比较。使用时间值的优点是,还可以使用大于和小于。不过,请确保先将时间设置为某个常用值,例如中午(12:00:00)或午夜(00:00:00)。

无论哪种方式,都可以编写一个函数,以您想要的方式格式化日期。

如果你不想要moment.js这样的库(它接管了所有内容),有很多格式化程序和解析器,例如Date.formatDate-format