Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a list of appointments structured like so (taken from JSON format):

var oData = {
    "data": {
        "d": {
            "results": [
                {
                    "Room": "403",
                    "Title": "MaLyssa Scott Meeting",
                    "Category": "Meeting",
                    "EventDate": "2016-10-31T19:30:00Z",
                    "EndDate": "2016-10-31T21:00:00Z"
                },
                {
                    "Room": "403",
                    "Title": "OF Upgrade Meeting",
                    "Category": "Meeting",
                    "EventDate": "2016-10-13T17:00:00Z",
                    "EndDate": "2016-10-13T18:00:00Z"
                },
                {
                    "Room": "428",
                    "Title": "IPAC DTID CRM",
                    "Category": "Meeting",
                    "EventDate": "2016-10-12T17:30:00Z",
                    "EndDate": "2016-10-12T18:30:00Z"
                },
                {
                    "Room": "434",
                    "Title": "BPR-12-001 Updates",
                    "Category": "Meeting",
                    "EventDate": "2016-10-10T15:00:00Z",
                    "EndDate": "2016-10-10T15:30:00Z"
                },
                {
                    "Room": "436",
                    "Title": "OF Update Meeting",
                    "Category": "Work hours",
                    "EventDate": "2016-10-12T17:00:00Z",
                    "EndDate": "2016-10-12T18:00:00Z"
                },
                {
                    "Room": "454",
                    "Title": "Real Property Meeting",
                    "Category": "Meeting",
                    "EventDate": "2016-10-12T20:00:00Z",
                    "EndDate": "2016-10-12T21:00:00Z"
                }
            ]
        }
    }
}      

I need to group the results array by Room Number, so for example, Room 403 has 2 children that are siblings of one another rather than 2 separate objects.

I figure I can do this with an 'if' statement and then push the objects into an array for each Room, whats a best practice to perform something like this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
267 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use reduce -

var newData = oData.data.d.results.reduce(function(prev,curr){
    if (!prev[curr.Room]) { // if room not already in the object add it.
        prev[curr.Room] = [];
    }
    prev[curr.Room].push(curr); 
    return prev;
}, {}); // init with an empty object

console.log(newData);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...