簡體   English   中英

遍歷對象數組,並在對象內組合 arrays

[英]Loop through array of objects, and combine arrays within objects

我如何遍歷對象的數據數組,然后將每個 guestList 數組連接成一個數組以供其他地方使用?

   const data = 
      [
        {
            "guestList": [
                {
                    "firstName": "test",
                    "surname": "test",
                    "email": "test@test.com",
                },
                {
                    "firstName": "test",
                    "surname": "tesT",
                    "email": "test@test.com",
                }
            ],
        },
        {
            "guestList": [
                {
                    "firstName": "test",
                    "surname": "test",
                    "email": "test@test.com",
                },
                {
                    "firstName": "test",
                    "surname": "tesT",
                    "email": "test@test.com",
                }
            ],
        },
        {
            "guestList": [
                {
                    "firstName": "test",
                    "surname": "test",
                    "email": "test@test.com",
                },
                {
                    "firstName": "test",
                    "surname": "tesT",
                    "email": "test@test.com",
                }
            ],
        }
      ]

您可以像這樣使用flatMap和解構:

 const data =[{"guestList": [{"firstName": "test","surname": "test","email": "test@test.com",},{"firstName": "test","surname": "tesT","email": "test@test.com",}],},{"guestList": [{"firstName": "test","surname": "test","email": "test@test.com",},{"firstName": "test","surname": "tesT","email": "test@test.com",}],},{"guestList": [{"firstName": "test","surname": "test","email": "test@test.com",},{"firstName": "test","surname": "tesT","email": "test@test.com",}],}]; const result = data.flatMap(({guestList}) => guestList); console.log(result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM