过滤深层嵌套的对象数组

时间:2018-08-23 09:29:07

标签: javascript arrays filter vuex

我有这个对象数组,里面还有其他数组。每个对象都是一个partner,每个伙伴都有1个或几个industriessolutions

我首先想做的是通过选定的partners过滤industry。例如,如果选择的下拉列表为Agency,那么我只想显示在其代理商价值内具有Agency作为选项的合作伙伴。

我对specialty的操作与此类似,但是我只能与之抗衡

export function getBySpecialty(partners, specialty) {
    if (!specialty) return partners
    return partners.filter(partner => partner.specialty === specialty)
}

如何为嵌套数组实现相同的目的?

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
      }
    ]
  }
];

2 个答案:

答案 0 :(得分:0)

尝试一下:

export function getByIndustry(partners, industryTitle) {
    if (!industryTitle) return partners
    return partners.filter(partner => partner.industries.filter(industry => industry.title === industryTitle).length)
}

答案 1 :(得分:0)

走了

export function getByIndustry(partners, industry) {
    if (!industry) return partners

    return partners.filter(partner => partner.industries.some(ind => ind.title === industry))
        .map(partner => partner)
}