[英]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.