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

Find if there is any subarray that do not match specified condition

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

checkDriversLicense功能必须经过组织,看每个单位是否至少有1人持有驾照。如果整个组织中有任何单位没有员工持有驾驶执照,则该单位必须returned。

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 ;

}

不过,此代码会检查所有单位,如果整个机构中只有一个人拥有驾照,则 return 为真(如果有其他单位持有驾照,则不 return 为假没有持有驾驶执照的工作人员)。 我怎样才能修改为 return 正确的结果?

我希望fasle被return编辑,因为子组织2子组织3没有员工有驾照。

虽然不是最紧凑的代码,但我认为这很容易理解

public checkDriversLicence(organisation: any[]) {
  for (let unit of organisation) {
    // each unit should have at least 1 driver's licence
    if (unit.staff.filter(e => e.driversLicenceA).length === 0) {
      // this unit does not have anyone with a driver's licence
      return false;
    }
  }
  // if we reach this point, each unit has at least one staff member with a driver's licence
  return true;
}

你可以用 Array#every for organisation abd with Array#some 来检查每个 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);

这里有些东西应该有所帮助。

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

          }
        ]
      },         
];

 // lets loop over the collection and try to acces the staff property!

organization.forEach(( or ) => { 
  drivers = or.staff; // these are our staff.

  // lets loop over the staffs and see if atleast one does not meet the criteria.
  // reduce function will allow us to iterate all the values and return a single value at the end.
  // we simply compare the value from each iteration to the previous value.
  // ie check if the current value is equal to the previous iteration value and return it.
  // The value returned at each iteration is used for next one and so on.
  // The code below assumes if any value at any iteration is false all values will be false.
  // at the end only values with true wil be the one that match our requirements.

  // check object 1 and 2
  let valid = drivers.reduce( ( accumulator , currentValue) => {
    return currentValue.driversLicenseA == accumulator;
  } );
    
  // lastly check if the value returned is as per our ask and print.    
  if(valid){
    console.log("Not applicable to Orgnization Id :" + or.id );
  }else{
   console.log("applicable to Organization Id :" +  or.id );
  }
        

});