首页 > 解决方案 > 使用函数将对象结构转换为不同的格式

问题描述

我有一个像这样的对象数组:

 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"
   }

];

我想将其转换为以下格式:

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"
  },
}
}

我编写了以下函数,试图用我想要的新结构创建一个新的对象数组:

var QuestionJson = require('./output');
var newQuestionArray =[];

for (let x=0;x<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"
  },
}
}
);
}

但是,此函数的输出会为某些键生成 [Object] 值:

[ { 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] } } ]

是什么导致了这个问题?我注意到“问题”键及其子对象显示正常 - 但在我的“答案”键上,我看到了这个 [Object] 值而不是我的对象!

任何帮助都会非常高兴!

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

标签: javascriptjson

解决方案


该功能运行良好。问题出在我终端的输出上。

Flapix 演示了通过使用 JSON stringify 函数和一些可选参数,该对象在终端中打印得很好。

json字符串化参考

console.log (JSON.stringify (newQuestionArray, null, 2);

推荐阅读