skip to Main Content

I have a nested array of objects that looks something like this:

[
 [
  {
   name: 'name',
   prettyFormat: 'joe bloggs'
   uselessInfo: 'drjfghsdr'
  },
  {
   name: 'email',
   answer: '[email protected]',
   uselessInfo: 'liodrgjiwe'
  }
 ]
 [
  {
   name: 'name',
   prettyFormat: 'mark obrien',
   uselessInfo: 'drjfghsdr'
  },
  {
   name: 'email',
   answer: 'mark [email protected]',
   uselessInfo: 'liodrgjiwe'
  }
 ]
]

I need to transform this array into something more sensible, actually a JSON object, with the following format:

{
  "LIST": [
    {
      "name": "joe bloggs",
      "email": "[email protected]",
    },
    {
      "name": "mark o'brien",
      "email": "mark [email protected]",

    },
  ]
}

This is my solution:

  let outputBuilder = []

  staffSubmissionsArray.forEach(subArray => {
    let personObj = {}
    subArray.forEach(obj => {
      if (obj.name == 'name') {
        personObj['name'] = obj.prettyFormat
      } if (obj.name == 'email') {
        personObj['email'] = obj.answer
      } 
      outputBuilder.push(personObj)
    })
  })

  console.log(JSON.stringify({ "LIST" : outputBuilder })

This gets everything into the right format, however, each person is getting duplicated, presumably each time I iterate over each object, but I don’t know how to fix this. Do I need to approach this in a different way?

3

Answers


  1. Perhaps you can try using map and reduce:

    const data = [
      [
        {
          name: 'name',
          prettyFormat: 'joe bloggs',
          uselessInfo: 'drjfghsdr'
        },
        {
          name: 'email',
          answer: '[email protected]',
          uselessInfo: 'liodrgjiwe'
        }
      ],
      [
        {
          name: 'name',
          prettyFormat: 'mark obrien',
          uselessInfo: 'drjfghsdr'
        },
        {
          name: 'email',
          answer: 'mark [email protected]',
          uselessInfo: 'liodrgjiwe'
        }
      ]
    ];
    const transformedData = {
      LIST: data.map(innerArray => 
        innerArray.reduce((acc, item) => {
          if (item.name === 'name') {
            acc.name = item.prettyFormat;
          } else if (item.name === 'email') {
            acc.email = item.answer;
          }
          return acc;
        }, {})
      )
    };
    console.log(JSON.stringify(transformedData, null, 2));
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  2. You can also use only the map method map(), together with the optional chaining .? and the nullish coalescing ?? operators:

    const data = [
      [
        {
          name: 'name',
          prettyFormat: 'joe bloggs',
          uselessInfo: 'drjfghsdr'
        },
        {
          name: 'email',
          answer: '[email protected]',
          uselessInfo: 'liodrgjiwe'
        }
      ],
      [
        {
          name: 'name',
          prettyFormat: 'mark obrien',
          uselessInfo: 'drjfghsdr'
        },
        {
          name: 'email',
          answer: 'mark [email protected]',
          uselessInfo: 'liodrgjiwe'
        }
      ]
    ];
    
    const transformedData = {"LIST": data.map(([obj1, obj2]) => ({
      name: obj1?.prettyFormat ?? obj2?.prettyFormat ?? "Name not found!",
      email: obj1?.answer ?? obj2?.answer ?? "Email not found!"
    }))};
    
    console.log(JSON.stringify(transformedData, null, 2));
    Login or Signup to reply.
  3. Another alternative is;

          const staffSubmissionsArray = [
            [
              {
                name: 'name',
                prettyFormat: 'joe bloggs',
                uselessInfo: 'drjfghsdr'
              },
              {
                name: 'email',
                answer: '[email protected]',
                uselessInfo: 'liodrgjiwe'
              }
            ],
            [
              {
                name: 'name',
                prettyFormat: 'mark obrien',
                uselessInfo: 'drjfghsdr'
              },
              {
                name: 'email',
                answer: 'mark [email protected]',
                uselessInfo: 'liodrgjiwe'
              }
            ]
          ];
    
          let outputBuilder = [];
    
          for (var i=0; i<staffSubmissionsArray.length; i++){
              outputBuilder.push(
                {
                  "name":  staffSubmissionsArray[i][0].prettyFormat, 
                  "email": staffSubmissionsArray[i][1].answer
                }
             )
           }
    
          console.log(JSON.stringify({ "LIST" : outputBuilder }))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search