如何使用 _.where(list, properties) 获取内部数组 属性?
How to use _.where(list, properties) to get inner array as property?
我有 JSON 结构如下
var listOfPlays = classRoom: [
{
title: "Dollhouse",
femaleLead: true,
student: [
{ name: "Echo", role: "doll" },
{ name: "Topher", role: "mad scientist" }
]
},
{
title: "Dr. Horrible's Sing-Along Blog",
student: [
{ name: "Billy", role: "mad scientist" },
{ name: "Penny", role: "love interest" }
]
}
]
我对 Underscore.js 中的 _.where 有基本的了解,它将查看列表中的每个值,returning 一个包含所有 [=] 的所有值的数组27=] 对在属性中列出。
例如 _.where(listOfPlays, {title: "Dollhouse"});
这将 return 我一个标题为 "Dollhouse" 的 object,但是我如何根据 [= 得到一个 object 24=]student 数组的值?来自 listOfPlays
?
我正在寻找类似的东西:
_.where(listOfPlays , {student: [name : "Echo"]});**
您正在寻找的_.where(listOfPlays , {student: [name : "Echo"]});
方式在新版本中不再适用。
您可以使用:
_.filter 查看列表中的每个值,返回通过真值测试(谓词)的所有值的数组
_.some 如果列表中的任何值通过谓词真值测试,则 returns 为真。
var listOfPlays = [{
title: "Dollhouse",
femaleLead: true,
student: [{
name: "Echo",
role: "doll"
},
{
name: "Topher",
role: "mad scientist"
}
]
},
{
title: "Dr. Horrible's Sing-Along Blog",
student: [{
name: "Billy",
role: "mad scientist"
},
{
name: "Penny",
role: "love interest"
}
]
}
]
var output = _.filter(listOfPlays, function(item) {
return _.some(item.student, {
name: "Echo"
});
});
console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
我有 JSON 结构如下
var listOfPlays = classRoom: [
{
title: "Dollhouse",
femaleLead: true,
student: [
{ name: "Echo", role: "doll" },
{ name: "Topher", role: "mad scientist" }
]
},
{
title: "Dr. Horrible's Sing-Along Blog",
student: [
{ name: "Billy", role: "mad scientist" },
{ name: "Penny", role: "love interest" }
]
}
]
我对 Underscore.js 中的 _.where 有基本的了解,它将查看列表中的每个值,returning 一个包含所有 [=] 的所有值的数组27=] 对在属性中列出。
例如 _.where(listOfPlays, {title: "Dollhouse"});
这将 return 我一个标题为 "Dollhouse" 的 object,但是我如何根据 [= 得到一个 object 24=]student 数组的值?来自 listOfPlays
?
我正在寻找类似的东西:
_.where(listOfPlays , {student: [name : "Echo"]});**
您正在寻找的_.where(listOfPlays , {student: [name : "Echo"]});
方式在新版本中不再适用。
您可以使用:
_.filter 查看列表中的每个值,返回通过真值测试(谓词)的所有值的数组
_.some 如果列表中的任何值通过谓词真值测试,则 returns 为真。
var listOfPlays = [{
title: "Dollhouse",
femaleLead: true,
student: [{
name: "Echo",
role: "doll"
},
{
name: "Topher",
role: "mad scientist"
}
]
},
{
title: "Dr. Horrible's Sing-Along Blog",
student: [{
name: "Billy",
role: "mad scientist"
},
{
name: "Penny",
role: "love interest"
}
]
}
]
var output = _.filter(listOfPlays, function(item) {
return _.some(item.student, {
name: "Echo"
});
});
console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>