查找是否存在不符合指定条件的子数组

阿尔法布拉沃查理...

我有一个名为orgnisation. 该数组包含组织中的单位。每个单元都是一个具有数组的对象staff一些工作人员持有 A 类驾驶执照 ( driversLicenseA)。

checkDriversLicense职能必须经过组织,看每个单位是否至少有一个人持有驾照。全组织有单位无持有驾驶证人员的,必须退还该单位。

organisation: [
  {
    id: 1,
    title: 'Sub Org 1',
    staff: [
      {
        id: 11,
        name: 'John',
        driversLicenseA: false,
      },
      {
        id: 12,
        name: 'Mike',
        driversLicenseA: true,
      },
      {
        id: 13,
        name: 'Daniel',
        driversLicenseA: false,
      }
    ]
  }
  ,
  {
    id: 2,
    title: 'Sub Org 2',
    staff: [
      {
        id: 21,
        name: 'Gustav',
        driversLicenseA: false,
      },
      {
        id: 22,
        name: 'Sandra',
        driversLicenseA: false,
      },
      {
        id: 23,
        name: 'Nikita',
        driversLicenseA: false,
      }
    ]
  },
  {
    id: 3,
    title: 'Sub Org 3',
    staff: [
      {
        id: 31,
        name: 'Carla',
        driversLicenseA: false,
      },
      {
        id: 32,
        name: 'Augustin',
        driversLicenseA: false,
      },
      {
        id: 33,
        name: 'Martin',
        driversLicenseA: false,

      }
    ]
  },
 
]

public checkDriversLicense(organisation: any[]) {
  let driversLicenseA =  organisation.find((element) => element.staff != null && element.staff?.length != 0 && element.staff?.some((item) => item.driversLicenseA == true));     
  return driversLicenseA ;

}

但是这个代码会检查所有单位,如果全机构有一个人有驾照,则返回true(如果其他单位没有人员持有驾照,则不返回false)。如何修改以返回正确的结果?

我想fasle被退回,因为Sub Org 2Sub Org 3没有拥有驾照的员工。

尼娜·舒尔茨

你可以检查Array#everyfor organisationabd with Array#somefor each staff

const
    organisation = [{ id: 1, title: 'Sub Org 1', staff: [{ id: 11, name: 'John', driversLicenseA: false }, { id: 12, name: 'Mike', driversLicenseA: true }, { id: 13, name: 'Daniel', driversLicenseA: false }] }, { id: 2, title: 'Sub Org 2', staff: [{ id: 21, name: 'Gustav', driversLicenseA: false }, { id: 22, name: 'Sandra', driversLicenseA: false }, { id: 23, name: 'Nikita', driversLicenseA: false }] }, { id: 3, title: 'Sub Org 3', units: [{ id: 31, name: 'Carla', driversLicenseA: false }, { id: 32, name: 'Augustin', driversLicenseA: false }, { id: 33, name: 'Martin', driversLicenseA: false }] }],
    eachOrganisationHasLicenceA = organisation.every(({ staff }) =>
        staff.some(({ driversLicenseA }) => driversLicenseA)
    );

console.log(eachOrganisationHasLicenceA);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章