• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

256
Views
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": "pkenny@cheese.co",
                    "role": "writer",
                    "displayName": "Bob Kenny"
                },
                {
                    "emailAddress": "nDrape@cheese.co",
                    "role": "writer",
                    "displayName": "Nute Drape"
                }
            ]
        },
        {
            "id": "149Lmt-g3w4w3efgh6thyherawer443awt3wrwa3rewrwa",
            "name": "Untitled document",
            "permissions": [
                {
                    "emailAddress": "pkenny@cheese.co",
                    "role": "owner",
                    "displayName": "Bob Kenny"
                }
            ]
        },
        {
            "id": "egrs54h6w4hgwe5wegrgwegrhterwwrttre-Uffk8QRg4",
            "name": "Documentation Control test",
            "permissions": [
                {
                    "emailAddress": "wDragon@cheese.co",
                    "role": "writer",
                    "displayName": "Grape Dragon"
                },
                {
                    "emailAddress": "pkenny@cheese.co",
                    "role": "owner",
                    "displayName": "Bob Kenny"
                }
            ]
        }
        
        ]

Second Snippet:

                        [
    {
        "1yKKftO0iOyvsacrW1mEr-tw43ttw3-8IorkDwiaYLgqI": [
            "pkenny@cheese.co",
            "nDrape@cheese.co"
        ]
    },
    {
        "149Lmt-g3w4w3efgh6thyherawer443awt3wrwa3rewrwa": [
            "pkenny@cheese.co"
        ]
    },
    {
        "egrs54h6w4hgwe5wegrgwegrhterwwrttre-Uffk8QRg4": [
            "wDragon@cheese.co",
            "pkenny@cheese.co"
        ]
    }
]
about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

   let resp = [{
       "id": "1yKKftO0iOyvsacrW1mEr-tw43ttw3-8IorkDwiaYLgqI",
       "name": "Doc Control",
       "permissions": [{
           "emailAddress": "pkenny@cheese.co",
           "role": "writer",
           "displayName": "Bob Kenny"
         },
         {
           "emailAddress": "nDrape@cheese.co",
           "role": "writer",
           "displayName": "Nute Drape"
         }
       ]
     },
     {
       "id": "149Lmt-g3w4w3efgh6thyherawer443awt3wrwa3rewrwa",
       "name": "Untitled document",
       "permissions": [{
         "emailAddress": "pkenny@cheese.co",
         "role": "owner",
         "displayName": "Bob Kenny"
       }]
     },
     {
       "id": "egrs54h6w4hgwe5wegrgwegrhterwwrttre-Uffk8QRg4",
       "name": "Documentation Control test",
       "permissions": [{
           "emailAddress": "wDragon@cheese.co",
           "role": "writer",
           "displayName": "Grape Dragon"
         },
         {
           "emailAddress": "pkenny@cheese.co",
           "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);

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error