简体   繁体   中英

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 .

What I'm trying to do in the first instance is filter the partners by the selected industry . 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.

I'm doing similar with a specialty but I only have a single value to contend with there

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM