how can js if else loop can be written in ts shortend technique

Viewed 20

I have this if loop , which set the isCustomerFunctionSelected as true and false and set value as null, how can i rewrite this in TS shortend technique.

  this.reset = false;

  if(this.functionValue === 'Customer') {
    this.isCustomerFunctionSelected = true;
  } else if (this.functionValue === 'Dealer') {
    this.isCustomerFunctionSelected = false;
    this.selectedCustomerValue = null;
  } else {
      this.reset = true;
  }
1 Answers

Won't shorten it, but will make it easier to maintain and evolve.

const {
  func,
} = ([{
  value: 'Customer',

  func: () => {
    this.isCustomerFunctionSelected = true;
  },
}, {
  value: 'Dealer',

  func: () => {
    this.isCustomerFunctionSelected = true;
    this.selectedCustomerValue = null;
  },
}].find(x => x.value === this.functionValue) || {
  func: () => {
    this.reset = true;
  },
});

func();
Related