How can I loop through the data array of objects, and then join each guestList array into a single array to be used elsewhere?
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:
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);