0

My MVC return this 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"
}
]

And i want winth Angular to group values by Timestamp part - hourly, daily or monthly

I want this result example for daily:

Tag | Timestamp | Value |

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

Can you give me any suggestion?

EDIT:

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

Community
  • 1
  • 1

1 Answers1

0

Try the code below :

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/

Sofiene Djebali
  • 4,398
  • 1
  • 21
  • 27