如何使用Angular过滤器按时间戳对数据进行分组

How can I group data with an Angular filter by timestamp?

本文关键字:数据 时间戳 何使用 Angular 过滤器      更新时间:2023-09-26

我的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 | 2015年10月26日| 4.5=(9/2)=值1+值2/计数

你能给我什么建议吗?

编辑:

我发现这给了我一些类似的开始方式:)

尝试以下代码:

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/