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": "[email protected]",
},
{
"firstName": "test",
"surname": "tesT",
"email": "[email protected]",
}
],
},
{
"guestList": [
{
"firstName": "test",
"surname": "test",
"email": "[email protected]",
},
{
"firstName": "test",
"surname": "tesT",
"email": "[email protected]",
}
],
},
{
"guestList": [
{
"firstName": "test",
"surname": "test",
"email": "[email protected]",
},
{
"firstName": "test",
"surname": "tesT",
"email": "[email protected]",
}
],
}
]
CodePudding user response:
You can use flatMap
and destructuring like this:
const data =[{"guestList": [{"firstName": "test","surname": "test","email": "[email protected]",},{"firstName": "test","surname": "tesT","email": "[email protected]",}],},{"guestList": [{"firstName": "test","surname": "test","email": "[email protected]",},{"firstName": "test","surname": "tesT","email": "[email protected]",}],},{"guestList": [{"firstName": "test","surname": "test","email": "[email protected]",},{"firstName": "test","surname": "tesT","email": "[email protected]",}],}];
const result = data.flatMap(({guestList}) => guestList);
console.log(result);