如何通过时间戳将角度过滤器数据分组?

时间:2015-10-28 11:40:50

标签: javascript angularjs asp.net-mvc

我的MVC返回此JSON

[
{
    "$id":"1",
    "Tags":
        [
            {"$id":"2","Values":
                            [
                                {"$id":"3","Tag":{"$ref":"2"},"ID":4,"TagID":1,"Value1":5.00,"TimeStamp":"2015-10-26T15:23:50","Quality":1,"Tags_ID":1},
                                {"$id":"4","Tag":{"$ref":"2"},"ID":7,"TagID":1,"Value1":4.00,"TimeStamp":"2015-10-26T15:25:50","Quality":2,"Tags_ID":1}
                            ],"Reports":[{"$ref":"1"}],"ID":1,"Name":"0101WT370/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null},
            {"$id":"5","Values":[],"Reports":[{"$ref":"1"}],"ID":2,"Name":"0101WT371/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null},
            {"$id":"6","Values":[],"Reports":[{"$ref":"1"}],"ID":3,"Name":"0101WT395/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null},
            {"$id":"7","Values":[],"Reports":[{"$ref":"1"}],"ID":4,"Name":"0101WT396/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null}

        ],
    "ID":3,"Name":"A"
},
{
    "$id":"8",
    "Tags":[],"ID":4,"Name":"B"
}
]

我希望winth Angular按时间戳分组值 - 每小时,每天或每月

我想要每天的结果示例:

标签|时间戳|价值|

Tag1中| 26-10-2015 | 4.5 =(9/2)= value1 + value2 / count

你能给我任何建议吗?

编辑:

I found this where give me something like way to start from :)

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

<强> JAVASCRIPT

    function sortTimeStamp(obj) {
    for(i=0; i<obj.length;i++) {
        for(z=0; z<obj[i].Tags.length;z++) {
            for(y=0; y<obj[i].Tags[z].Values.length;y++) {
                obj[i].Tags[z].Values.sort(function (a, b) {
                  return new Date(b.TimeStamp).getTime() - new Date(a.TimeStamp).getTime();
                });
            } 
        }  
    }
    return obj;
}

var myObj = [
    {
        "$id": "1",
        "Tags": [
            {
                "$id": "2",
                "Values": [
                    {
                        "$id": "3",
                        "Tag": {
                            "$ref": "2"
                        },
                        "ID": 4,
                        "TagID": 1,
                        "Value1": 5,
                        "TimeStamp": "2015-10-26T15:23:50",
                        "Quality": 1,
                        "Tags_ID": 1
                    },
                    {
                        "$id": "4",
                        "Tag": {
                            "$ref": "2"
                        },
                        "ID": 7,
                        "TagID": 1,
                        "Value1": 4,
                        "TimeStamp": "2015-10-26T15:25:50",
                        "Quality": 2,
                        "Tags_ID": 1
                    }
                ],
                "Reports": [
                    {
                        "$ref": "1"
                    }
                ],
                "ID": 1,
                "Name": "0101WT370/WT.VALUE",
                "Type": null,
                "Archive": null,
                "Server": null,
                "Created": null,
                "Edited": null
            },
            {
                "$id": "5",
                "Values": [],
                "Reports": [
                    {
                        "$ref": "1"
                    }
                ],
                "ID": 2,
                "Name": "0101WT371/WT.VALUE",
                "Type": null,
                "Archive": null,
                "Server": null,
                "Created": null,
                "Edited": null
            },
            {
                "$id": "6",
                "Values": [],
                "Reports": [
                    {
                        "$ref": "1"
                    }
                ],
                "ID": 3,
                "Name": "0101WT395/WT.VALUE",
                "Type": null,
                "Archive": null,
                "Server": null,
                "Created": null,
                "Edited": null
            },
            {
                "$id": "7",
                "Values": [],
                "Reports": [
                    {
                        "$ref": "1"
                    }
                ],
                "ID": 4,
                "Name": "0101WT396/WT.VALUE",
                "Type": null,
                "Archive": null,
                "Server": null,
                "Created": null,
                "Edited": null
            }
        ],
        "ID": 3,
        "Name": "A"
    },
    {
        "$id": "8",
        "Tags": [],
        "ID": 4,
        "Name": "B"
    }
];

myObj = sortTimeStamp(myObj);

console.log(myObj);

https://jsfiddle.net/3y7wfqwq/