javascript - 使用函数将对象结构转换为不同的格式
问题描述
我有一个像这样的对象数组:
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)
解决方案
该功能运行良好。问题出在我终端的输出上。
Flapix 演示了通过使用 JSON stringify 函数和一些可选参数,该对象在终端中打印得很好。
console.log (JSON.stringify (newQuestionArray, null, 2);
推荐阅读
- firebase-realtime-database - 创建 Firebase 登录并且不要更改 FirebaseAuth
- r - 编辑每个其他值的最终数字
- rdf - What is the difference between rdf:resource and rdfs:Resource?
- c# - Openxml,excel截断前导零
- javascript - 什么可能导致 DynamoDB 表扫描既不返回数据又不返回错误?
- javascript - Prevent file from generating new build - Webpack
- r - y * 权重错误:二元运算符的非数字参数
- c++ - 使用嵌套的 while 循环查找二维数组的数组长度
- python - Django - 如何序列化继承
- reactjs - 如何修复无法解决模块反应 redux hooks\useDispatch?