我从JSON格式的数据库中检索数据,并将其分配给变量 - ' var tabs'。
格式化的JSON如下:
[{
"TAB_ID": "1",
"TAB_NAME": "TAB ONE",
"TAB_DISPLAY": "1",
"QUESTIONS": [{
"QUESTION_ID": 1,
"QUESTION": "Question number one",
"ANSWER": ""
}, {
"QUESTION_ID": 2,
"QUESTION": "Question number two",
"ANSWER": ""
}, {
"QUESTION_ID": 3,
"QUESTION": "Question number six",
"ANSWER": ""
}]
}, {
"TAB_ID": "1",
"TAB_NAME": "TAB ONE",
"TAB_DISPLAY": "1",
"QUESTIONS": [{
"QUESTION_ID": 1,
"QUESTION": "Question number one",
"ANSWER": "Some Other Answer"
}, {
"QUESTION_ID": 2,
"QUESTION": "Question number two",
"ANSWER": ""
}, {
"QUESTION_ID": 3,
"QUESTION": "Question number six",
"ANSWER": "Some Still Different Answer"
}]
}]
我需要迭代数组并执行以下操作:
对于TAB中的每个QUESTION,如果QUESTION有答案,我需要添加一个额外的键/值" HAS_ANSWER":" 1"问题。
{
"QUESTION_ID": 1,
"QUESTION": "Question number one",
"ANSWER": "Some Other Answer",
"HAS_ANSWER": "1"
}
我花了好几个小时阅读帖子,查看示例代码和阅读Underscore文档,我知道这应该相当简单,但我无法理解如何处理这个问题。
我的理解是我可能想要使用嵌套的_.map函数,但我所看到的所有示例都返回了对象数组的子集,而不是修改后的扩展,我需要最终得到完全相同的格式化JSON数据(对象数组),但上面提到的更改和添加。
非常感谢任何帮助。
更新
使用此:
data = _.map(data, obj => _.map(obj.QUESTIONS, q => {
if (q.ANSWER) {
q.HAS_ANSWER = 1;
}
return q;
}));
这会迭代问题,并相应地修改它们,但返回的对象会丢弃外层数据 - 它只返回一组问题,并且不会保留TAB数据:
"TAB_ID": "1",
"TAB_NAME": "TAB ONE",
"TAB_DISPLAY": "1",
解决方案是在外部数组上使用_.each,在内部数组上使用_.map。
data = _.each(data, obj => _.map(obj.QUESTIONS, q => {
if (q.ANSWER) {
q.HAS_ANSWER = 1;
}
return q;
}));
注意:这=>语法仅适用于Javascript的较新实现,并且目前在Safari / Webkit中不受支持,因此请使用此语法以获得更好的兼容性
data = _.each(data, function(obj){
_.map(obj.QUESTIONS, function(q){
if (q.ANSWER) {
q.HAS_ANSWER = 1;
}
return q;
});
});
这完全按我需要的方式处理事情。
答案 0 :(得分:1)
这可以使用普通的JavaScript,使用Array#map
。
// Iterate over data
data = data.map(obj => obj.QUESTIONS.map(q => {
// If question has answer, then add `HAS_ANSWER` property
if (q.ANSWER) {
q.HAS_ANSWER = 1;
}
// Update question object
return q;
}));
var data = [{
"TAB_ID": "1",
"TAB_NAME": "TAB ONE",
"TAB_DISPLAY": "1",
"QUESTIONS": [{
"QUESTION_ID": 1,
"QUESTION": "Question number one",
"ANSWER": ""
}, {
"QUESTION_ID": 2,
"QUESTION": "Question number two",
"ANSWER": ""
}, {
"QUESTION_ID": 3,
"QUESTION": "Question number six",
"ANSWER": ""
}]
}, {
"TAB_ID": "1",
"TAB_NAME": "TAB ONE",
"TAB_DISPLAY": "1",
"QUESTIONS": [{
"QUESTION_ID": 1,
"QUESTION": "Question number one",
"ANSWER": "Some Other Answer"
}, {
"QUESTION_ID": 2,
"QUESTION": "Question number two",
"ANSWER": ""
}, {
"QUESTION_ID": 3,
"QUESTION": "Question number six",
"ANSWER": "Some Still Different Answer"
}]
}];
data = data.map(obj => obj.QUESTIONS.map(q => {
if (q.ANSWER) {
q.HAS_ANSWER = 1;
}
return q;
}));
console.log(data);
document.getElementById('output').innerHTML = JSON.stringify(data, 0, 4);
<pre id="output"></pre>
Underscore / Lodash中的相同代码
data = _.map(data, obj => _.map(obj.QUESTIONS, q => {
if (q.ANSWER) {
q.HAS_ANSWER = 1;
}
return q;
}));
答案 1 :(得分:1)
使用标准的Javascript&#39; Array.prototype.forEach()方法,它看起来像这样:
% echo aaaj | awk '{gsub(/a*|j/,"x");print}'
xx
tabs.forEach(tab => {
tab['QUESTIONS'].forEach(question => {
if (question['ANSWER']) {
question['HAS_ANSWER'] = 1;
}
});
});
&#13;
这种方法可以修改数据。