在下面的代码中,“答案”键存在于第二和第四对象中,但是当我运行代码时,我只是得到第二个对象作为输出。
预期输出是需要显示“答案”的对象
var users = [{
"answerLength": "10",
"isComment": "false",
"question": "what?",
"uniqueId": "01"
}, {
"answerLength": "10",
"isComment": "false",
"question": "what?",
"uniqueId": "aaa",
"answer": "02"
}, {
"answerLength": "10",
"isComment": "false",
"question": "what?",
"uniqueId": "03"
}, {
"answerLength": "10",
"isComment": "false",
"question": "what?",
"uniqueId": "aaa",
"answer": "04"
}];
function checkAnsExist(){
var forEach = _.find(users, _.forEach(function(i){ return i.answer}));
document.write(JSON.stringify(forEach));
}
checkAnsExist();
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.core.min.js"></script>
最佳答案
您要查找的是filter
,而不是find
var users = [{
"answerLength": "10",
"isComment": "false",
"question": "what?",
"uniqueId": "01"
}, {
"answerLength": "10",
"isComment": "false",
"question": "what?",
"uniqueId": "aaa",
"answer": "02"
}, {
"answerLength": "10",
"isComment": "false",
"question": "what?",
"uniqueId": "03"
}, {
"answerLength": "10",
"isComment": "false",
"question": "what?",
"uniqueId": "aaa",
"answer": "04"
}];
function checkAnsExist(){
var forEach = _.filter(users, 'answer');
document.write(JSON.stringify(forEach));
}
checkAnsExist();
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.core.min.js"></script>
关于javascript - Lodash查找并显示呈现的json数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42597963/