从嵌套的json创建对象

时间:2018-07-21 18:26:35

标签: javascript json typescript

我从服务器以角度接收了以下json,我想从此Json创建一个自定义对象

A1:A20

我想要得到的想要的结果应该是:

{
    "showsHall": [
        {
            "movies": [
                "5b428ceb9d5b8e4228d14225",
                "5b428d229d5b8e4228d14226",
            ],
            "shows": [
                "5b428d6b9d5b8e4228d14229",
                "5b428ef69d5b8e4228d1422a",
            ],
            "_id": "5b428d439d5b8e4228d14228",
            "hallName": "Small hall",
            "rows": 10,
            "seats": 40,
        }
    ],
    "movie": [
        {
            "showsHalls": [
                "5b428d439d5b8e4228d14228",
                "5b43490592bd380e30fef376"
            ],
            "shows": [
                "5b428d6b9d5b8e4228d14229",
                "5b466d9d75a55841c48bacfc"
            ],
            "_id": "5b428ceb9d5b8e4228d14225",
            "movieName": "Moonwalker - Michael jackson",
            "directorName": "Jerry Kramer, Colin Chilvers",
            "movieDescription": "Anthology movie by, and starring, Michael 
             Jackson in his prime, combining a number of...",
        }
    ],
    "takenSeats": [],
    "_id": "5b428d6b9d5b8e4228d14229",
    "showDate": "7/10/2018",
    "showStartTime": "4:00 PM",
    "showEndTime": "6:00 PM",
}

任何帮助将不胜感激,谢谢

1 个答案:

答案 0 :(得分:0)

假设原始对象名为source,则可以按以下步骤组装新对象:

 const newObject = {       
    "hallName": source.showsHall[0].hallName,
    "movieName": source.movie[0].movieName,
    "takenSeats": source.takenSeats,
    "_id": source._id,
    "showDate": source.showDate,
    "showStartTime": source.showStartTime,
    "showEndTime": source.showEndTime
}

// have a look at it
console.log(newObject);