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

Time:03-05

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": "[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"
                }
            ]
        }
        
        ]

Second Snippet:

                        [
    {
        "1yKKftO0iOyvsacrW1mEr-tw43ttw3-8IorkDwiaYLgqI": [
            "[email protected]",
            "[email protected]"
        ]
    },
    {
        "149Lmt-g3w4w3efgh6thyherawer443awt3wrwa3rewrwa": [
            "[email protected]"
        ]
    },
    {
        "egrs54h6w4hgwe5wegrgwegrhterwwrttre-Uffk8QRg4": [
            "[email protected]",
            "[email protected]"
        ]
    }
]

CodePudding user response:

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

  • Related