Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - How can I restructure a JS object?

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

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

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...