【问题标题】:Javascript add missing days time series resultsJavascript 添加缺失天数时间序列结果
【发布时间】:2015-03-28 10:02:13
【问题描述】:

我有一个针对 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
    }
]

我需要用零值添加缺失的日期,例如,如果我要求从 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-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 尝试了一些东西,但我无法得到正确的结果。

有什么帮助吗?

【问题讨论】:

  • 您在哪里遇到问题?
  • 我不明白如何用下划线来做,我有一个包含所有日期的数组,但我不能将它与 mongodb 结果相交。
  • 你也可以发布那个数组吗?
  • 已发,请看,谢谢

标签: javascript date underscore.js time-series


【解决方案1】:

我做到了,但我错过了添加零值,我现在要这样做。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 2023-03-08
    • 2021-08-11
    • 2016-07-02
    • 2013-11-27
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多