[英]Filtering a deeply nested array of objects
I have this array of objects with other arrays inside.我有这个对象数组,里面有其他数组。 Each object is a partner
and each partner has 1 or several industries
and solutions
.每个对象都是一个partner
,每个合作伙伴都有一个或多个industries
和solutions
。
What I'm trying to do in the first instance is filter the partners
by the selected industry
.我首先要做的是按所选industry
过滤partners
。 As an example, if the dropdown chosen was Agency
then I only want to display the partners that have Agency
as an option within their agency value.例如,如果选择的下拉列表是Agency
那么我只想显示在其代理值中将Agency
作为选项的合作伙伴。
I'm doing similar with a specialty
but I only have a single value to contend with there我正在做类似的specialty
但我只有一个价值要与之抗衡
export function getBySpecialty(partners, specialty) {
if (!specialty) return partners
return partners.filter(partner => partner.specialty === specialty)
}
How can I achieve the same thing for a nested array?如何为嵌套数组实现相同的功能?
var partners = [
{
id: 24,
logo: null,
name: "Partner 1",
slug: "partner-1",
excerpt:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sit amet massa tellus, ac faucibus urna. Donec eget risus elit, quis pellentesque purus. Ut purus nibh, elementum ac ullamcorper consequat, vehicula et risus.",
solutions: [
{
groupId: 2,
id: 22,
tempId: null,
uid: "c2ebbe02-0028-4a2e-9233-41fbece3b3c7",
fieldLayoutId: 20,
structureId: null,
contentId: 22,
enabled: true,
archived: false,
siteId: 1,
enabledForSite: true,
title: "Solution 1",
slug: "solution-1",
uri: null,
dateCreated: {
date: "2018-08-22 02:25:48.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
dateUpdated: {
date: "2018-08-22 02:25:48.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
root: null,
lft: null,
rgt: null,
level: null,
searchScore: null,
awaitingFieldValues: false,
propagating: false
},
{
groupId: 2,
id: 23,
tempId: null,
uid: "830d531e-807c-4aae-827d-170e21b72e32",
fieldLayoutId: 20,
structureId: null,
contentId: 23,
enabled: true,
archived: false,
siteId: 1,
enabledForSite: true,
title: "Solution 2",
slug: "solution-2",
uri: null,
dateCreated: {
date: "2018-08-22 02:25:52.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
dateUpdated: {
date: "2018-08-22 02:25:52.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
root: null,
lft: null,
rgt: null,
level: null,
searchScore: null,
awaitingFieldValues: false,
propagating: false
}
],
specialty: "Behavioral Analytics",
industries: [
{
groupId: 3,
id: 15,
tempId: null,
uid: "87b17403-0aec-4ecf-a73f-c70aefb0c3ed",
fieldLayoutId: 21,
structureId: null,
contentId: 15,
enabled: true,
archived: false,
siteId: 1,
enabledForSite: true,
title: "Aerospace",
slug: "aerospace",
uri: null,
dateCreated: {
date: "2018-08-21 06:59:57.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
dateUpdated: {
date: "2018-08-21 06:59:57.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
root: null,
lft: null,
rgt: null,
level: null,
searchScore: null,
awaitingFieldValues: false,
propagating: false
}
]
},
{
id: 9,
logo: null,
name: "Partner 2",
slug: "partner-2",
excerpt:
"A little excerpt about the partner",
solutions: [
{
groupId: 2,
id: 12,
tempId: null,
uid: "c93cb6c7-d44f-42cc-b170-8f0799b9a9f2",
fieldLayoutId: 20,
structureId: null,
contentId: 12,
enabled: true,
archived: false,
siteId: 1,
enabledForSite: true,
title: "Data Agility & Flexibility",
slug: "data-agility-flexibility",
uri: null,
dateCreated: {
date: "2018-08-21 06:59:34.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
dateUpdated: {
date: "2018-08-21 06:59:34.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
root: null,
lft: null,
rgt: null,
level: null,
searchScore: null,
awaitingFieldValues: false,
propagating: false
},
{
groupId: 2,
id: 13,
tempId: null,
uid: "eabaf4aa-e69b-4e38-adac-df6ad9099eae",
fieldLayoutId: 20,
structureId: null,
contentId: 13,
enabled: true,
archived: false,
siteId: 1,
enabledForSite: true,
title: "Unifed Customer Data",
slug: "unifed-customer-data",
uri: null,
dateCreated: {
date: "2018-08-21 06:59:43.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
dateUpdated: {
date: "2018-08-21 06:59:43.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
root: null,
lft: null,
rgt: null,
level: null,
searchScore: null,
awaitingFieldValues: false,
propagating: false
}
],
specialty: "Contextual Location",
industries: [
{
groupId: 3,
id: 15,
tempId: null,
uid: "87b17403-0aec-4ecf-a73f-c70aefb0c3ed",
fieldLayoutId: 21,
structureId: null,
contentId: 15,
enabled: true,
archived: false,
siteId: 1,
enabledForSite: true,
title: "Aerospace",
slug: "aerospace",
uri: null,
dateCreated: {
date: "2018-08-21 06:59:57.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
dateUpdated: {
date: "2018-08-21 06:59:57.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
root: null,
lft: null,
rgt: null,
level: null,
searchScore: null,
awaitingFieldValues: false,
propagating: false
},
{
groupId: 3,
id: 16,
tempId: null,
uid: "cfe1349a-e7c4-420f-8db3-5a453243777f",
fieldLayoutId: 21,
structureId: null,
contentId: 16,
enabled: true,
archived: false,
siteId: 1,
enabledForSite: true,
title: "Agency",
slug: "agency",
uri: null,
dateCreated: {
date: "2018-08-21 07:00:09.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
dateUpdated: {
date: "2018-08-21 07:00:09.000000",
timezone_type: 3,
timezone: "America/Los_Angeles"
},
root: null,
lft: null,
rgt: null,
level: null,
searchScore: null,
awaitingFieldValues: false,
propagating: false
}
]
}
];
Try this:尝试这个:
export function getByIndustry(partners, industryTitle) {
if (!industryTitle) return partners
return partners.filter(partner => partner.industries.filter(industry => industry.title === industryTitle).length)
}
Gone with消失了
export function getByIndustry(partners, industry) {
if (!industry) return partners
return partners.filter(partner => partner.industries.some(ind => ind.title === industry))
.map(partner => partner)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.