Home > other >  How can I reduce the length of my conditionals in my JavaScript?
How can I reduce the length of my conditionals in my JavaScript?

Time:12-30

let getSlotList = \[\];
    if (
      res.data.hasOwnProperty('alley') &&
      res.data.hasOwnProperty('booked') &&
      res.data.hasOwnProperty('reminder')
    ) {
      getSlotList = \[
        ...res.data.alley,
        ...res.data.booked,
        ...res.data.reminder,
      \];
    } else if (
      res.data.hasOwnProperty('alley') &&
      res.data.hasOwnProperty('booked')
    ) {
      getSlotList = \[...res.data.alley, ...res.data.booked\];
    } else if (
      res.data.hasOwnProperty('booked') &&
      res.data.hasOwnProperty('reminder')
    ) {
      getSlotList = \[...res.data.reminder, ...res.data.booked\];
    } else if (
      res.data.hasOwnProperty('alley') &&
      res.data.hasOwnProperty('reminder')
    ) {
      getSlotList = \[...res.data.alley, ...res.data.booked\];
    } else if (res.data.hasOwnProperty('alley')) {
      getSlotList = \[...res.data.alley\];
    } else if (res.data.hasOwnProperty('reminder')) {
      getSlotList = \[...res.data.reminder\];
    } else if (res.data.hasOwnProperty('booked')) {
      getSlotList = \[...res.data.booked\];
    }

    console.log(
      '           
  • Related