我正在尝试比较JSON中的年龄和性别,并尝试打印相应的名称以获取详细信息。但它也不适合我。
_.each(response.details, function(value, key){
if (value.age == "12" && value.gender == "male") {
console.log(value.name + "name")
}
});
JSON:
details: [
{
age: "12",
gender: "male",
name: "danny"
},
{
age: "13",
gender: "male",
name: "sunny"
},
{
age: "12",
gender: "female",
name: "janny"
},
{
age: "14",
gender: "female",
name: "josef"
}
],
答案 0 :(得分:0)
您的代码正常运行,我添加了_.result()
和_.find()
函数:
var details = [{
age: "12",
gender: "male",
name: "danny"
}, {
age: "13",
gender: "male",
name: "sunny"
}, {
age: "12",
gender: "female",
name: "janny"
}, {
age: "14",
gender: "female",
name: "josef"
}];
console.log(_.result(_.find(details, {
age: '12',
gender: 'male'
}), 'name') + 'name');

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.js"></script>
&#13;
答案 1 :(得分:0)
为什么要使用lodash,javascript通过过滤器原生提供此支持!
var details = [{
age: "12",
gender: "male",
name: "danny"
}, {
age: "13",
gender: "male",
name: "sunny"
}, {
age: "12",
gender: "female",
name: "janny"
}, {
age: "14",
gender: "female",
name: "josef"
}];
var results = details.filter(function(row) {
if (row.age == "12" && row.gender == "male") {
return true;
}
});
results.forEach(function(result) {
console.log(result.name + ' name');
})