Javascript添加缺少的天数时间序列结果

Javascript添加缺少的天数时间序列结果,javascript,date,underscore.js,time-series,Javascript,Date,Underscore.js,Time Series,我有一个针对Mongodb的聚合操作的结果对象,结果如下: [ { "_id": { "action": "hit", "date": "2015-01-20T00:00:00.000Z" }, "avg": 3, "min": 3, "max": 3, "total": 3 }, { "_id": {

我有一个针对Mongodb的聚合操作的结果对象,结果如下:

[
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-20T00:00:00.000Z"
        },
        "avg": 3,
        "min": 3,
        "max": 3,
        "total": 3
    },
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-23T00:00:00.000Z"
        },
        "avg": 1,
        "min": 1,
        "max": 1,
        "total": 12
    }
]
[
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-20T00:00:00.000Z"
        },
        "avg": 3,
        "min": 3,
        "max": 3,
        "total": 3
    },
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-21T00:00:00.000Z"
        },
        "avg": 0,
        "min": 0,
        "max": 0,
        "total": 0
    },
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-22T00:00:00.000Z"
        },
        "avg": 0,
        "min": 0,
        "max": 0,
        "total": 0
    },
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-23T00:00:00.000Z"
        },
        "avg": 1,
        "min": 1,
        "max": 1,
        "total": 12
    },
    {...}
]
我需要用零值添加缺少的日期,因此,例如,如果我要求从1月20日开始的一周范围,我希望有这样一个结果对象:

[
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-20T00:00:00.000Z"
        },
        "avg": 3,
        "min": 3,
        "max": 3,
        "total": 3
    },
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-23T00:00:00.000Z"
        },
        "avg": 1,
        "min": 1,
        "max": 1,
        "total": 12
    }
]
[
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-20T00:00:00.000Z"
        },
        "avg": 3,
        "min": 3,
        "max": 3,
        "total": 3
    },
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-21T00:00:00.000Z"
        },
        "avg": 0,
        "min": 0,
        "max": 0,
        "total": 0
    },
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-22T00:00:00.000Z"
        },
        "avg": 0,
        "min": 0,
        "max": 0,
        "total": 0
    },
    {
        "_id": {
            "action": "hit",
            "date": "2015-01-23T00:00:00.000Z"
        },
        "avg": 1,
        "min": 1,
        "max": 1,
        "total": 12
    },
    {...}
]
日期范围为的数组如下所示:

[ 
"2015-01-20T00:00:00.000Z",
"2015-01-21T00:00:00.000Z",
"2015-01-22T00:00:00.000Z",
"2015-01-23T00:00:00.000Z",
"2015-01-24T00:00:00.000Z",
"2015-01-25T00:00:00.000Z",
"2015-01-26T00:00:00.000Z",
]
对于此数组中的每个日期,我必须在以前的结果中添加一个零值对象

我尝试了使用下划线和map/reduce/clone的方法,但无法得到正确的结果


有什么帮助吗?

我做了,但是我没有添加零值,我现在要做

        var cloneItem = {};

        // Generate the date range arrays with single days
        var range = moment().range(new Date(req.query.datefrom), new Date(req.query.dateto)),
            days = [];
        range.by('days', function(moment) {
            days.push(moment.startOf('day').format());
        });

        // Rewrite dates to match the same date range format from the array above
        webstat = _.map(webstat, function (value, key, list) {
            value._id.date = moment(value._id.date).startOf('day').format();
            if (key === 0) cloneItem = value;
            return value;
        });

        // Add the object values when they are missed               
        days.forEach(function (day) {
            var clone = _.extend({}, cloneItem, {_id: {action: cloneItem._id.action, date: day}});

            if (_.findWhere(webstat, {_id: { date: day}}) === undefined) {
                webstat.push(clone);
            }
        });

        // Sort by date and send it
        res.jsonp(
            _.sortBy(webstat, function (item) {
                return item._id.date;
            })
        );

你哪里有问题?我不知道如何使用下划线,我有一个数组,其中包含了结果中应该包含的所有日期,但我无法将其与mongodb结果相交。你能不能也发布该数组。发布了,请查看,谢谢