title: Convert Object structure to different format using function
body: <p>I have an array of objects like so: </p>
  
  <pre><code> const questions = [
      {
      "id":1,    
      "question":"What animal does bacon come from?",
      "correctAnswer": "A",
      "A":"pig",
      "B":"cow",
      "C":"sheep"
      },
     {
      "id":2,
      "question":"Which planet is closest to the sun?",
      "correctAnswer": "B",
      "A":"Saturn",
      "B":"Mercury",
      "C":"Venus"
     }
  
  ];
  </code></pre>
  
  <p>I want to convert it into the following format: </p>
  
  <pre><code>var newQuestionStructure = {
  id: 1,
  question:{ Voice: "what animal does bacon come from?", Display:"what animal does bacon come from?"},
  correctAnswer:"A",
  answers:
  {
    A:
    {
      Voice: "pig" , 
      Display:"pig" ,
      Letter:"A"
    },
    B:
    {
      Voice: "cow" , 
      Display:"cow" ,
      Letter:"B"
    },
    C:
    {
      Voice: "sheep" , 
      Display:"sheep" ,
      Letter:"C"
    },
  }
  }
  </code></pre>
  
  <p>I wrote the following function as an attempt to create a new array of objects with my new desired structure: </p>
  
  <pre><code>var QuestionJson = require('./output');
  var newQuestionArray =[];
  
  for (let x=0;x&lt;QuestionJson.length;x++){
  newQuestionArray.push({
  id: QuestionJson[x].id,
  question: { Voice:QuestionJson[x].A, Display:QuestionJson[x].question},
  correctAnswer:QuestionJson[x].correctAnswer,
  answers:
  {
    A:
    {
      Voice: QuestionJson[x].A , 
      Display:QuestionJson[x].A ,
      Letter:"A"
    },
    B:
    {
      Voice: QuestionJson[x].B , 
      Display:QuestionJson[x].B ,
      Letter:"B"
    },
    C:
    {
      Voice: QuestionJson[x].C , 
      Display:QuestionJson[x].C ,
      Letter:"C"
    },
  }
  }
  );
  }
  </code></pre>
  
  <p>However, the output of this function produces [Object] values for some of the keys:</p>
  
  <pre><code>[ { id: 1,
      question:
       { Voice: 'What animal does bacon come from?',
         Display: 'What animal does bacon come from?' },
      correctAnswer: 'A',
      answers: { A: [Object], B: [Object], C: [Object] } },
    { id: 2,
      question:
       { Voice: 'Which planet is closest to the sun?',
         Display: 'Which planet is closest to the sun?' },
      correctAnswer: 'B',
      answers: { A: [Object], B: [Object], C: [Object] } } ]
  </code></pre>
  
  <p>What is causing this issue? I noticed that the "question" key and its sub object display fine - but on my "answers" key I see this [Object] value instead of my object! </p>
  
  <p>Any help would be great cheers!</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var QuestionJson = [{
      "id": 1,
      "question": "What animal does bacon come from?",
      "correctAnswer": "A",
      "A": "pig",
      "B": "cow",
      "C": "sheep"
    },
    {
      "id": 2,
      "question": "Which planet is closest to the sun?",
      "correctAnswer": "B",
      "A": "Saturn",
      "B": "Mercury",
      "C": "Venus"
    }
  
  ];
  
  var newQuestionArray = [];
  
  for (let x = 0; x &lt; QuestionJson.length; x++) {
    newQuestionArray.push({
      id: QuestionJson[x].id,
      question: {
        Voice: QuestionJson[x].A,
        Display: QuestionJson[x].question
      },
      correctAnswer: QuestionJson[x].correctAnswer,
      answers: {
        A: {
          Voice: QuestionJson[x].A,
          Display: QuestionJson[x].A,
          Letter: "A"
        },
        B: {
          Voice: QuestionJson[x].B,
          Display: QuestionJson[x].B,
          Letter: "B"
        },
        C: {
          Voice: QuestionJson[x].C,
          Display: QuestionJson[x].C,
          Letter: "C"
        },
      }
    });
  }
  
  console.log(newQuestionArray)</code></pre>
  </div>
  </div>
  </p>