我有这个 JSON 对象数组
"questions": [
{
"id": "id",
"answer":"answer",
"question_type": "q_type",
"question": {
"header":"Text",
"question_image": "link",
"answers_list": ["array"],
}
},
{
"id": "id",
"answer":"answer",
"question_type": "q_type",
"question": {
"header": "Text",
"question_image": "link",
"choices_type": "text",
"choices": ["array"]
}
}
]
我想从这个 json 中提取另一个包含 id 和答案的 json,就像这样
"answers": [{"question": question_id, "answer": "text"}]
最佳答案
您可以使用 JavaScript 映射函数(假设变量 q 包含您的问题对象):
var out = q.questions.map(function(element) {
return {"question": element.id, "answer": element.answer};
});
关于javascript - 创建一个新的 JSON 数组,其中包含来自另一个 JSON 的特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34444563/