Loop through array of objects, and combine arrays within objects

BennKingy

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]",
                }
            ],
        }
      ]
trincot

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Loop through Array of Objects within Objects in JavaScript

Loop through array of arrays containing objects

Loop through 2 arrays containing array and array of objects - Php

How to loop through properties of objects inside of an array within a React component?

Loop through array contain objects and create different arrays from it

How to loop through an array of objects, that's within another array of objects - javascript

JS - How do I loop through arrays within other arrays of objects

Combine 2 objects from 2 arrays in a loop?

Struggling to loop through an array of objects

Loop through json array with objects

Loop through an array of objects and display the objects within a html/css defined table

Loop through array of arrays which contain objects and get all specific values from the objects then show to the page JQuery

Combine arrays of identical length into array of objects

Combine several arrays into an array of objects in JavaScript

How to combine two arrays into an array of objects in javascript?

How to combine two arrays into an array of objects?

How to combine two arrays into one array of objects?

Javascript - Loop through nested arrays and objects filter

Loop through objects containing arrays in AngularJS

Javascript Objects - Check for and loop through arrays in object

Loop through two arrays of objects Javascript

How to loop through multiple arrays within objects in a JSON API using JavaScript

Loop through objects within objects to create nested menu?

Combine unknown sub-arrays of objects from an array of objects and filter it

How to Filter objects and arrays within Javascript Array

How to Loop through JSON Objects having Objects and Arrays Inside

React map through an array of objects and arrays

Loop through array of objects against another array of objects

JS Loop through an array of objects of an array of objects to delete entry