I have an array called orgnisation
. This array entails units in the organisation. Each unit is an object that has an array staff
. Some staff have drivers license type A (driversLicenseA
).
checkDriversLicense
function must go through organisation and see if there is at least one person holding driver's license in each unit. If there is any unit in the whole organisation that has no staff holding driver's license, that unit has to be 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 ;
}
This code however checkes all units and if there is one person in the whole organsation that has driver's license, returns true (false is not returned if there are other units that have no staff holding deriver's license). How can I modify to return correct result?
I want fasle
be returned because Sub Org 2 and Sub Org 3 have no staff with driver's license.