如何使用日期数组javascript创建计数对象数组

时间:2016-06-23 10:18:05

标签: javascript arrays

我的javascript数组。

startDate = 2015-12-01;

但我的搜索日期为endDate = 2016-09-30;startDate我希望在endDatevar newOjArray = [ {"2015-12":"0"}, {"2016-01":"3"}, {"2016-02":"3"}, {"2016-03":"3"}, {"2016-04":"3"}, {"2016-05":"3"}, {"2016-06":"3"}, {"2016-07":"0"}, {"2016-08":"1"}, {"2016-09":"0"} ]; 之间获得新的日期数组。这个新数组将显示如下,

var data = Enumerable.Range(1, 100).Select(i => new TestDto()
{
       Id = i,
}).AsQueryable();

值表示考虑日期范围的总计数。我是如何创造它的。

4 个答案:

答案 0 :(得分:1)

完整的提案。使用具有所需分组结果的数组。

function getGroupedData(dates, from, to) {
    function pad(s, n) { return s.toString().length < n ? pad('0' + s, n) : s; }

    var temp = Object.create(null),
        result = [],
        fromYear = +from.slice(0, 4),
        fromMonth = +from.slice(5, 7),
        toYear = +to.slice(0, 4),
        toMonth = +to.slice(5, 7),
        o, k;

    datearray.forEach(function (d) {
        var k = d.slice(0, 7);
        temp[k] = (temp[k] || 0) + 1;
    });

    while (true) {
        k = pad(fromYear, 4) + '-' + pad(fromMonth, 2);
        o = {};
        o[k] = (temp[k] || 0).toString();
        result.push(o);
        if (fromYear === toYear && fromMonth === toMonth) {
            break;
        }
        fromMonth++;
        if (fromMonth > 12) {
            fromMonth = 1;
            fromYear++;
        }
    }
    return result;
}

var datearray = ["2016-01-13", "2016-01-18", "2016-01-30", "2016-02-13", "2016-02-18", "2016-02-28", "2016-03-13", "2016-03-23", "2016-03-30", "2016-04-13", "2016-04-18", "2016-04-30", "2016-05-13", "2016-05-18", "2016-05-28", "2016-06-13", "2016-06-23", "2016-06-30", "2016-08-22"];

console.log(getGroupedData(datearray, '2015-12-01', '2016-09-30'));

答案 1 :(得分:0)

您可以使用Array.filter来过滤此数组。利用您的特定日期格式,我们不需要进行任何日期算术,我们可以简单地将日期比较为字符串,并使用localeCompare()来比较它们:

var datearray = [
    "2016-01-13", 
    "2016-01-18", 
    "2016-01-30", 
    "2016-02-13", 
    "2016-02-18", 
    "2016-02-28", 
    "2016-03-13", 
    "2016-03-23", 
    "2016-03-30", 
    "2016-04-13", 
    "2016-04-18", 
    "2016-04-30", 
    "2016-05-13", 
    "2016-05-18", 
    "2016-05-28", 
    "2016-06-13", 
    "2016-06-23", 
    "2016-06-30", 
    "2016-08-22"
];

var startDate = "2015-12-01"; 
var endDate = "2016-01-30";

var filteredArray = datearray.filter(function(item){
  return item.localeCompare( startDate ) > -1 && endDate.localeCompare( item ) > -1;
});

console.log( filteredArray );

现在,您拥有filteredArray,您可以简单地遍历它以计算一个月内的日期数。

答案 2 :(得分:0)

你可以试试这个:

Underscore.js已用于操纵数据。

&#13;
&#13;
var datearray=["2016-01-13","2016-01-18","2016-01-30","2016-02-13","2016-02-18","2016-02-28","2016-03-13","2016-03-23","2016-03-30","2016-04-13","2016-04-18","2016-04-30","2016-05-13","2016-05-18","2016-05-28","2016-06-13","2016-06-23","2016-06-30","2016-08-22"];

var boxingDay = new Date("12/01/2015");
var nextWeek  = new Date("09/30/2016");

function getDates( d1, d2 ){
  var oneDay = 24*3600*1000;
  for (var d=[],ms=d1*1,last=d2*1;ms<last;ms+=oneDay){
var new_Date=new Date(ms);
    d.push( new_Date.getFullYear()+"-"+("0" + (new_Date.getMonth() + 1)).slice(-2) );
  }
  return d;
}

var x=[];
_.each(datearray, function(e){x.push(e.substring(0, 7));});

var z= _.uniq(getDates( boxingDay, nextWeek ));

var f=x.concat(_.uniq(getDates( boxingDay, nextWeek )));

document.getElementById("xx").innerHTML=JSON.stringify(_.countBy(f));
&#13;
<script src="http://underscorejs.org/underscore-min.js"></script>

<div id="xx"></div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如果您正在寻找更多ES6方式,请查看:

var dateArray = ["2016-01-13", "2016-01-18", "2016-01-30", "2016-02-13", "2016-02-18", "2016-02-28", "2016-03-13", "2016-03-23", "2016-03-30", "2016-04-13", "2016-04-18", "2016-04-30", "2016-05-13", "2016-05-18", "2016-05-28", "2016-06-13", "2016-06-23", "2016-06-30", "2016-08-22"];
var group = {};

dateArray.forEach(date =>
  group[(date = date.substr(0, 7))] =
  (group[date] || []).concat(date)
);
var result = Object.keys(group)
  .map(date => ({
    [date]: group[date].length
  }));

console.log(result)

如果您的日期格式是日期数组,那么最简单的方法是使用substr如果长度不是常数,那么您可以通过spacer将其拆分,然后得到两个第一个值。如果它完全是一个日期字符串,您可以从中创建一个日期并将其转换为您想要的字符串作为对象的键。