JS-过滤嵌套的对象数组

时间:2019-04-28 19:19:41

标签: javascript arrays

样本JSON数据:

const data = [{
    "_id": "5cc5df25a193c21608666b00",
    "departmentName": "Software Dev",
    "managerId": "5cc5df25a193c21608666b01",
    "managerEmail": "swdmanager@company.com",
    "teams": [{
        "teamName": "SWD Team 1",
        "teamLeadId": "5cc5df25a193c21608666b02",
        "teamLeadEmail": "swdteam1lead@company.com",
        "teamMembers": [{
          "memberId": "5cc5df25a193c21608666b03",
          "memberEmail": "JonDoe1@company.com"
        }]
      },
      {
        "teamName": "SWD Team 2",
        "teamLeadId": "5cc5df25a193c21608666b04",
        "teamLeadEmail": "swdteam2lead@company.com",
        "teamMembers": [{
            "memberId": "5cc5df25a193c21608666b05",
            "memberEmail": "JonDoe2@company.com"
          },
          {
            "memberId": "5cc5df25a193c21608666b06",
            "memberEmail": "JonDoe3@company.com"
          }
        ]
      }
    ]
  },
  {
    "_id": "5cc5df25a193c21608666b07",
    "departmentName": "Software QA",
    "managerId": "5cc5df25a193c21608666b08",
    "managerEmail": "qamanager@company.com",
    "teams": [{
        "teamName": "QA Team 1",
        "teamLeadId": "5cc5df25a193c21608666b09",
        "teamLeadEmail": "qateam1lead@company.com",
        "teamMembers": [{
          "memberId": "5cc5df25a193c21608666b10",
          "memberEmail": "JonDoe4@company.com"
        }]
      },
      {
        "teamName": "QA Team 2",
        "teamLeadId": "5cc5df25a193c21608666b11",
        "teamLeadEmail": "qateam2lead@company.com",
        "teamMembers": []
      },
      {
        "teamName": "QA Team 3",
        "teamLeadId": "5cc5df25a193c21608666b12",
        "teamLeadEmail": "qateam3lead@company.com",
        "teamMembers": [{
            "memberId": "5cc5df25a193c21608666b13",
            "memberEmail": "JonDoe4@company.com"
          },
          {
            "memberId": "5cc5df25a193c21608666b14",
            "memberEmail": "JonDoe5@company.com"
          },
          {
            "memberId": "5cc5df25a193c21608666b15",
            "memberEmail": "JonDoe6@company.com"
          }
        ]
      }
    ]
  },
  {
    "_id": "5cc5df25a193c21608666b16",
    "departmentName": "Software Creative",
    "managerId": "5cc5df25a193c21608666b17",
    "managerEmail": "swcmanager@company.com",
    "teams": []
  },
  {
    "_id": "5cc5df25a193c21608666b18",
    "departmentName": "Software BA",
    "managerId": "5cc5df25a193c21608666b19",
    "managerEmail": "swbamanager@company.com",
    "teams": [{
      "teamName": "BA Team 1",
      "teamLeadId": "5cc5df25a193c21608666b20",
      "teamLeadEmail": "bateam3lead@company.com",
      "teamMembers": []
    }]
  }
];

jsFiddle

描述:共有三个级别。上层=部门,中层=团队,下层=团队成员。一个部门可以有0个或更多的团队。一个团队可以有0个或更多的团队成员。

目标::我试图仅返回teamMembers数组中至少填充了一个值的数据。

因此,输出中应消除的是1)软件质量检查/质量检查团队2-仅质量检查团队2,因为其他质量检查团队都有teamMembers 2)软件创意-整个对象,因为它甚至没有团队,更不用说teamMember了,以及3)Software BA-拥有一个团队,但没有teamMembers。这三个对象在teamMembers数组中没有填充单个值。

我觉得我需要使用Array.prototype.filter(),但不确定如何使用嵌套对象(#JSnewbie)。另外,我现在正在学习JS,所以一些建议以最新的方式来实现这一点非常棒。

所需的输出:

[
  {
    "_id": "5cc5df25a193c21608666b00",
    "departmentName": "Software Dev",
    "managerId": "5cc5df25a193c21608666b01",
    "managerEmail": "swdmanager@company.com",
    "teams": [
      {
        "teamName": "SWD Team 1",
        "teamLeadId": "5cc5df25a193c21608666b02",
        "teamLeadEmail": "swdteam1lead@company.com",
        "teamMembers": [
          {
            "memberId": "5cc5df25a193c21608666b03",
            "memberEmail": "JonDoe1@company.com"
          }
        ]
      },
      {
        "teamName": "SWD Team 2",
        "teamLeadId": "5cc5df25a193c21608666b04",
        "teamLeadEmail": "swdteam2lead@company.com",
        "teamMembers": [
          {
            "memberId": "5cc5df25a193c21608666b05",
            "memberEmail": "JonDoe2@company.com"
          },
          {
            "memberId": "5cc5df25a193c21608666b06",
            "memberEmail": "JonDoe3@company.com"
          }
        ]
      }
    ]
  },
  {
    "_id": "5cc5df25a193c21608666b07",
    "departmentName": "Software QA",
    "managerId": "5cc5df25a193c21608666b08",
    "managerEmail": "qamanager@company.com",
    "teams": [
      {
        "teamName": "QA Team 1",
        "teamLeadId": "5cc5df25a193c21608666b09",
        "teamLeadEmail": "qateam1lead@company.com",
        "teamMembers": [
          {
            "memberId": "5cc5df25a193c21608666b10",
            "memberEmail": "JonDoe4@company.com"
          }
        ]
      },
      {
        "teamName": "QA Team 3",
        "teamLeadId": "5cc5df25a193c21608666b12",
        "teamLeadEmail": "qateam3lead@company.com",
        "teamMembers": [
          {
            "memberId": "5cc5df25a193c21608666b13",
            "memberEmail": "JonDoe4@company.com"
          },
          {
            "memberId": "5cc5df25a193c21608666b14",
            "memberEmail": "JonDoe5@company.com"
          },
          {
            "memberId": "5cc5df25a193c21608666b15",
            "memberEmail": "JonDoe6@company.com"
          }
        ]
      }
    ]
  }
]

3 个答案:

答案 0 :(得分:2)

您可以减少数组,并使用递归方法检查并返回嵌套属性的数组。

Thge函数Array.find接受一个数据数组和一个嵌套属性键的数组。如果想要的键的数组包含一些项目,它将返回一个包含项目的数组。

如果还有更多可用的键,则将移交并检查带有数组的实际属性。如果结果不是空数组,则将获取实际对象的副本以及过滤的结果,并将该对象推送到结果集中。

如果没有更多键可用,则将未更改的对象用作结果集。

filter
function filter(array, [key, ...keys]) {
    return array.reduce((r, o) => {
        if (o[key] && o[key].length) {
            if (keys.length) {
                var temp = filter(o[key], keys);
                if (temp.length) r.push(Object.assign({}, o, { [key]: temp }));
            } else {
                r.push(o);
            }
        }
        return r;
    }, []);
}

var data = [{ _id: "5cc5df25a193c21608666b00", departmentName: "Software Dev", managerId: "5cc5df25a193c21608666b01", managerEmail: "swdmanager@company.com", teams: [{ teamName: "SWD Team 1", teamLeadId: "5cc5df25a193c21608666b02", teamLeadEmail: "swdteam1lead@company.com", teamMembers: [{ memberId: "5cc5df25a193c21608666b03", memberEmail: "JonDoe1@company.com" }] }, { teamName: "SWD Team 2", teamLeadId: "5cc5df25a193c21608666b04", teamLeadEmail: "swdteam2lead@company.com", teamMembers: [{ memberId: "5cc5df25a193c21608666b05", memberEmail: "JonDoe2@company.com" }, { memberId: "5cc5df25a193c21608666b06", memberEmail: "JonDoe3@company.com" }] }] }, { _id: "5cc5df25a193c21608666b07", departmentName: "Software QA", managerId: "5cc5df25a193c21608666b08", managerEmail: "qamanager@company.com", teams: [{ teamName: "QA Team 1", teamLeadId: "5cc5df25a193c21608666b09", teamLeadEmail: "qateam1lead@company.com", teamMembers: [{ memberId: "5cc5df25a193c21608666b10", memberEmail: "JonDoe4@company.com" }] }, { teamName: "QA Team 2", teamLeadId: "5cc5df25a193c21608666b11", teamLeadEmail: "qateam2lead@company.com", teamMembers: [] }, { teamName: "QA Team 3", teamLeadId: "5cc5df25a193c21608666b12", teamLeadEmail: "qateam3lead@company.com", teamMembers: [{ memberId: "5cc5df25a193c21608666b13", memberEmail: "JonDoe4@company.com" }, { memberId: "5cc5df25a193c21608666b14", memberEmail: "JonDoe5@company.com" }, { memberId: "5cc5df25a193c21608666b15", memberEmail: "JonDoe6@company.com" }] }] }, { _id: "5cc5df25a193c21608666b16", departmentName: "Software Creative", managerId: "5cc5df25a193c21608666b17", managerEmail: "swcmanager@company.com", teams: [] }, { _id: "5cc5df25a193c21608666b18", departmentName: "Software BA", managerId: "5cc5df25a193c21608666b19", managerEmail: "swbamanager@company.com", teams: [{ teamName: "BA Team 1", teamLeadId: "5cc5df25a193c21608666b20", teamLeadEmail: "bateam3lead@company.com", teamMembers: [] }] }],
    result = filter(data, ['teams', 'teamMembers']);

console.log(result);

答案 1 :(得分:1)

您可以过滤没有队伍的队伍,然后将它们映射到一个新数组,以过滤空队伍。

首先,您必须过滤完全没有任何团队的团队。但是您将剩下没有团队成员的团队。因此,您必须使用.map仅添加具有团队成员的团队。

const filterEmptyTeams = (data) => data.filter((department) => {
  const teams = department.teams.filter((team) => team.teamMembers.length > 0)
  return teams.length !== 0
}).map((department) => {
 const teams = department.teams.filter((team) =>      team.teamMembers.length > 0)
  return {...department, teams}
})

const data = [{
    "_id": "5cc5df25a193c21608666b00",
    "departmentName": "Software Dev",
    "managerId": "5cc5df25a193c21608666b01",
    "managerEmail": "swdmanager@company.com",
    "teams": [{
        "teamName": "SWD Team 1",
        "teamLeadId": "5cc5df25a193c21608666b02",
        "teamLeadEmail": "swdteam1lead@company.com",
        "teamMembers": [{
          "memberId": "5cc5df25a193c21608666b03",
          "memberEmail": "JonDoe1@company.com"
        }]
      },
      {
        "teamName": "SWD Team 2",
        "teamLeadId": "5cc5df25a193c21608666b04",
        "teamLeadEmail": "swdteam2lead@company.com",
        "teamMembers": [{
            "memberId": "5cc5df25a193c21608666b05",
            "memberEmail": "JonDoe2@company.com"
          },
          {
            "memberId": "5cc5df25a193c21608666b06",
            "memberEmail": "JonDoe3@company.com"
          }
        ]
      }
    ]
  },
  {
    "_id": "5cc5df25a193c21608666b07",
    "departmentName": "Software QA",
    "managerId": "5cc5df25a193c21608666b08",
    "managerEmail": "qamanager@company.com",
    "teams": [{
        "teamName": "QA Team 1",
        "teamLeadId": "5cc5df25a193c21608666b09",
        "teamLeadEmail": "qateam1lead@company.com",
        "teamMembers": [{
          "memberId": "5cc5df25a193c21608666b10",
          "memberEmail": "JonDoe4@company.com"
        }]
      },
      {
        "teamName": "QA Team 2",
        "teamLeadId": "5cc5df25a193c21608666b11",
        "teamLeadEmail": "qateam2lead@company.com",
        "teamMembers": []
      },
      {
        "teamName": "QA Team 3",
        "teamLeadId": "5cc5df25a193c21608666b12",
        "teamLeadEmail": "qateam3lead@company.com",
        "teamMembers": [{
            "memberId": "5cc5df25a193c21608666b13",
            "memberEmail": "JonDoe4@company.com"
          },
          {
            "memberId": "5cc5df25a193c21608666b14",
            "memberEmail": "JonDoe5@company.com"
          },
          {
            "memberId": "5cc5df25a193c21608666b15",
            "memberEmail": "JonDoe6@company.com"
          }
        ]
      }
    ]
  },
  {
    "_id": "5cc5df25a193c21608666b16",
    "departmentName": "Software Creative",
    "managerId": "5cc5df25a193c21608666b17",
    "managerEmail": "swcmanager@company.com",
    "teams": []
  },
  {
    "_id": "5cc5df25a193c21608666b18",
    "departmentName": "Software BA",
    "managerId": "5cc5df25a193c21608666b19",
    "managerEmail": "swbamanager@company.com",
    "teams": [{
      "teamName": "BA Team 1",
      "teamLeadId": "5cc5df25a193c21608666b20",
      "teamLeadEmail": "bateam3lead@company.com",
      "teamMembers": []
    }]
  }
];

console.log(filterEmptyTeams(data))

答案 2 :(得分:1)

这里是带有两个嵌套.reduce()

的版本
const result = data.reduce(
  (acc, e) => {
    if (e.teams && e.teams.length > 0) {
      const teams = e.teams.reduce(
        (acc, e) => {
          if (e.teamMembers && e.teamMembers.length > 0) {
            acc.push({...e});
          }
          return acc;
        },
        [],
      );
      if (teams.length > 0) {
        acc.push({...e, teams});
      }
    }
    return acc
  },
  [],
);