Nested JSON, create new array with values from multiple levels?

I have this nested json object but I would like to construct a new one by taking elements of the original. I would like to make the second array below using the first, where the id key value becomes the key for a list of the emailAddress values in each permissionsOriginal:

        [
        {
            "id": "1yKKftO0iOyvsacrW1mEr-tw43ttw3-8IorkDwiaYLgqI",
            "name": "Doc Control",
            "permissions": [
                {
                    "emailAddress": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f0f141a1111063f1c171a1a0c1a511c10">[email protected]</a>",
                    "role": "writer",
                    "displayName": "Bob Kenny"
                },
                {
                    "emailAddress": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c52784e5d4c597c5f5459594f59125f53">[email protected]</a>",
                    "role": "writer",
                    "displayName": "Nute Drape"
                }
            ]
        },
        {
            "id": "149Lmt-g3w4w3efgh6thyherawer443awt3wrwa3rewrwa",
            "name": "Untitled document",
            "permissions": [
                {
                    "emailAddress": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f7879c9299998eb7949f92928492d99498">[email protected]</a>",
                    "role": "owner",
                    "displayName": "Bob Kenny"
                }
            ]
        },
        {
            "id": "egrs54h6w4hgwe5wegrgwegrhterwwrttre-Uffk8QRg4",
            "name": "Documentation Control test",
            "permissions": [
                {
                    "emailAddress": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cabd8eb8abada5a48aa9a2afafb9afe4a9a5">[email protected]</a>",
                    "role": "writer",
                    "displayName": "Grape Dragon"
                },
                {
                    "emailAddress": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83f3e8e6ededfac3e0ebe6e6f0e6ade0ec">[email protected]</a>",
                    "role": "owner",
                    "displayName": "Bob Kenny"
                }
            ]
        }
        
        ]

Second Snippet:

                        [
    {
        "1yKKftO0iOyvsacrW1mEr-tw43ttw3-8IorkDwiaYLgqI": [
            "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4d4cfc1cacadde4c7ccc1c1d7c18ac7cb">[email protected]</a>",
            "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="49270d3b28392c092a212c2c3a2c672a26">[email protected]</a>"
        ]
    },
    {
        "149Lmt-g3w4w3efgh6thyherawer443awt3wrwa3rewrwa": [
            "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99e9f2fcf7f7e0d9faf1fcfceafcb7faf6">[email protected]</a>"
        ]
    },
    {
        "egrs54h6w4hgwe5wegrgwegrhterwwrttre-Uffk8QRg4": [
            "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="691e2d1b080e0607290a010c0c1a0c470a06">[email protected]</a>",
            "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fffe4eae1e1f6cfece7eaeafceaa1ece0">[email protected]</a>"
        ]
    }
]

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Loop through each element, then loop through permissions and extract the email.

   let resp = [{
       "id": "1yKKftO0iOyvsacrW1mEr-tw43ttw3-8IorkDwiaYLgqI",
       "name": "Doc Control",
       "permissions": [{
           "emailAddress": "[email protected]",
           "role": "writer",
           "displayName": "Bob Kenny"
         },
         {
           "emailAddress": "[email protected]",
           "role": "writer",
           "displayName": "Nute Drape"
         }
       ]
     },
     {
       "id": "149Lmt-g3w4w3efgh6thyherawer443awt3wrwa3rewrwa",
       "name": "Untitled document",
       "permissions": [{
         "emailAddress": "[email protected]",
         "role": "owner",
         "displayName": "Bob Kenny"
       }]
     },
     {
       "id": "egrs54h6w4hgwe5wegrgwegrhterwwrttre-Uffk8QRg4",
       "name": "Documentation Control test",
       "permissions": [{
           "emailAddress": "[email protected]",
           "role": "writer",
           "displayName": "Grape Dragon"
         },
         {
           "emailAddress": "[email protected]",
           "role": "owner",
           "displayName": "Bob Kenny"
         }
       ]
     }

   ]
   let res = [];
   resp.forEach((data) => {
     let emailArray = [];
     let resObj = {};
     data.permissions.forEach((permData) => {
       emailArray.push(permData['emailAddress']);
     })
     resObj[data.id] = emailArray;
     res.push(resObj);
   })
   console.log(res);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x