javascript - 如何构造一个包含对象数组的输出?
问题描述
假设我有这样的样本数据,
const questions_chklist =
{ credit_broker_checklist:
[ { id: '1', title: 'Are any identified risks to the customer given equal prominence?', question_id: 'wmBHr' }
, { id: '2', title: 'Content has been approved by an authorised individual', question_id: 'sPYvu' }
, { id: '3', title: 'Does it clearly name your company or the ?', question_id: '64bBL' }
]
, test_checklist_checklist:
[ { id: '1', title: 'new questionn 1', question_id: '0VsnL' }
, { id: '2', title: 'new question 02', question_id: 'c9jrW' }
, { id: '3', title: 'New question 03', question_id: 'fbJON' }
, { id: '4', title: 'new question 412234', question_id: 'AbcDE' }
]
, new_list_checklist: [ ]
}
现在我正在尝试创建一个这样的对象,
{ credit_broker_checklist:
[ { answer: '', comments: '', question_id: 'wmBHr' }
, { answer: '', comments: '', question_id: 'sPYvu' }
, { answer: '', comments: '', question_id: '64bBL' }
]
, test_checklist_checklist:
[ { answer: '', comments: '', question_id: '0VsnL' }
, { answer: '', comments: '', question_id: 'c9jrW' }
, { answer: '', comments: '', question_id: 'fbJON' }
, { answer: '', comments: '', question_id: 'AbcDE' }
]
, new_list_checklist: [ ]
}
目前,我不知道如何生成一个对象,所以我是这样生成的,
const questions_chklist =
{ credit_broker_checklist:
[ { id: '1', title: 'Are any identified risks to the customer given equal prominence?', question_id: 'wmBHr' }
, { id: '2', title: 'Content has been approved by an authorised individual', question_id: 'sPYvu' }
, { id: '3', title: 'Does it clearly name your company or the ?', question_id: '64bBL' }
]
, test_checklist_checklist:
[ { id: '1', title: 'new questionn 1', question_id: '0VsnL' }
, { id: '2', title: 'new question 02', question_id: 'c9jrW' }
, { id: '3', title: 'New question 03', question_id: 'fbJON' }
, { id: '4', title: 'new question 412234', question_id: 'AbcDE' }
]
, new_list_checklist: [ ]
}
let jsonObj = []
for (var key in questions_chklist)
{
if (questions_chklist.hasOwnProperty(key))
{
let option_count = questions_chklist[key].length;
for (let i = 0; i < option_count; i++)
{
let item = {}
item ["answer"] = "";
item ["comments"] = "";
item["question_id"] = questions_chklist[key][i].question_id
jsonObj.push(item);
}
}
}
console.log( jsonObj )
.as-console-wrapper { max-height: 100% !important; top: 0 }
它只是创建一个由几个对象组成的数组,
[
{
answer: '',
comments: '',
question_id: 'wmBHr'
},
{
answer: '',
comments: '',
question_id: 'sPYvu'
},
...
....
解决方案
如果您希望 jsonObj 输出为对象,请不要将其初始化为数组。您所做的是遍历每个属性的值并将项目推送到 jsonObj 数组中。
如果我正确理解了您的问题,则可以对您的代码进行一些调整以输出您需要的内容。
let questions_chklist = {
"credit_broker_checklist": [
{
"id": "1",
"title": "Are any identified risks to the customer given equal prominence?",
"question_id": "wmBHr"
},
{
"id": "2",
"title": "Content has been approved by an authorised individual",
"question_id": "sPYvu"
},
{
"id": "3",
"title": "Does it clearly name your company or the ?",
"question_id": "64bBL"
}],
"test_checklist_checklist": [
{
"id": "1",
"title": "new questionn 1",
"question_id": "0VsnL"
},
{
"id": "2",
"title": "new question 02",
"question_id": "c9jrW"
},
{
"id": "3",
"title": "New question 03",
"question_id": "fbJON"
},
{
"id": "4",
"title": "new question 412234",
"question_id": "AbcDE"
}
],
"new_list_checklist": []
}
let jsonObj = {}
for (var key in questions_chklist) {
if (questions_chklist.hasOwnProperty(key)) {
jsonObj[key] = [];
let option_count = questions_chklist[key].length;
for (let i = 0; i < option_count; i++) {
let item = {}
item ["answer"] = "";
item ["comments"] = "";
item["question_id"] = questions_chklist[key][i].question_id
jsonObj[key].push(item);
}
}
}
console.log(jsonObj)
推荐阅读
- java - Rest Assured-java.net.ConnectException:连接被拒绝:连接
- linux - 在一个命令中动态杀死多个 Mac OS 进程?
- c# - 无法使用 IronOCR dll 将 PDF 文件转换为文本
- java - 重构 If 语句以获得更好的可读性
- function - Jasmine:测试一个方法是否被另一个方法调用
- html - 为什么行中的元素在响应式布局的苹果邮件代理上折叠?
- gcc - Gcc 给出 [1] 504 ...这是什么?
- r - How to group by with if statement in R?
- python - 如何通过 Selenium 和 Python 使用用户代理在 youtube 评论中单击 youtube 链接
- javascript - 无法访问 web 动态项目中的 angular js 页面