• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

279
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda