下划线做一些计算和 return 新对象

Underscore do some calculation and return new object

我有一个这样的时间序列:

[
    {
        "_id": {
            "action": "click",
            "date": "2015-02-02T00:00:00+01:00"
        },
        "total": 5
    },
    {
        "_id": {
            "action": "hit",
            "date": "2015-02-02T00:00:00+01:00"
        },
        "total": 13
    },
    {
        "_id": {
            "action": "hit",
            "date": "2015-02-03T00:00:00+01:00"
        },
        "total": 25
    },
    {
        "_id": {
            "action": "click",
            "date": "2015-02-03T00:00:00+01:00"
        },
        "total": 7
    }
]

我需要做一些计算和return一个相似的对象,特别是,我需要找到点击和点击之间的百分比(同一日期的clicks/hits*100)return像这样创建一个对象:

[
    {
        "_id": {
            "action": "ctr",
            "date": "2015-02-02T00:00:00+01:00"
        },
        "total": 38.46
    },
    {
        "_id": {
            "action": "ctr",
            "date": "2015-02-03T00:00:00+01:00"
        },
        "total": 28
    }
]

有什么提示吗?

_.groupBy(array, function(el){ return el._id.date; });

以上就是您的分组方式,然后您可以对其进行映射并计算您需要的内容。示例:

http://jsfiddle.net/j9Htk/114/