1

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.

3 Answers3

3

You could check with Array#every for organisation abd with Array#some for 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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Though not the most compact code possible, I think this is clear to understand

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

fravolt
  • 2,565
  • 1
  • 4
  • 19
0

Here is something that should help.

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

});
RohitS
  • 1,036
  • 12
  • 17