简体   繁体   English

遍历对象数组,并在对象内组合 arrays

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

How can I loop through the data array of objects, and then join each guestList array into a single array to be used elsewhere?我如何遍历对象的数据数组,然后将每个 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",
                }
            ],
        }
      ]

You can use flatMap and destructuring like this:您可以像这样使用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