简体   繁体   中英

How to improve this routine with lodash?

I have written some code I am not proud of and as a lodash noob I would appreciate some feedback on how to improve this code.

The requirement is to populate an array of timeslots with defaults for certain types of discounts, eg: 在此处输入图片说明

For example, there are 6 tables for every discount (top row) for each timeslots going down on the left. At 12:00pm there are 2 tables in the 20% discount column etc. On the right hand side are totals for each timeslot.

I currently have the data in this format:

class DiscountTimeTable {
    public restaurantScheduleDiscountId: number;
    public scheduleDiscountId: number;
    public name: string;
    public tables: number;
}

class TablesForTimesAndDiscount {
    public restaurantScheduleDayTimeId: number;
    public timeSlot: string;
    public discounts: Array<DiscountTimeTable>;
}

And then I do a foreach loop down the timeslots and then foreach timeslot, another foreach loop across through all discounts. Then, if the discount matches XI check if that time and discount is in another array (discount20Times) and if it is, do something.

The code works, but I am sure it is inefficient - I just don't know what approach to take with Lodash to improve it?

public resetDefaults(): void {
        let discount10Times: string[] = ['PT13H30M', 'PT16H30M', 'PT19H30M', 'PT22H30M'];
        let discount20Times: string[] = ['PT12H', 'PT12H30M', 'PT13H', 'PT17H', 'PT17H30M', 'PT18H', 'PT18H30M', 'PT19H', 'PT23H'];

    _.forEach(this.tablesForTimesAndDiscounts, (tablesForTimesAndDiscount: TablesForTimesAndDiscount) => {
        _.forEach(tablesForTimesAndDiscount.discounts, (discount: DiscountTimeTable) => {
            if (discount.scheduleDiscountId === 0) {
                discount.tables = 6;  //regardless of time - all tables
            }
            if (discount.scheduleDiscountId === 10) {
                if (_.find(discount10Times, (time: string) => time == tablesForTimesAndDiscount.timeSlot)) {
                    discount.tables = 2;
                }
            }
            if (discount.scheduleDiscountId === 20) {
                if (_.find(discount20Times, (time: string) => time == tablesForTimesAndDiscount.timeSlot)) {
                    discount.tables = 2;
                }
            }
        });
    });
}

you can immediately speed up lookups by using an object instead of an array ( the type declarations might be wrong, but you get the idea)

I shortened one of the variable names just to make it easier to read

public resetDefaults(): void {
  let discount10Times: Object<string> = {
    PT13H30M: 'PT13H30M', 
    PT16H30M: 'PT16H30M',
    PT19H30M: 'PT22H30M'
  };
  let discount20Times: Object<string> = {
    PT12H:    'PT12H', 
    PT12H30M: 'PT12H30M',
    PT13H:    'PT13H',
    PT17H:    'PT17H',
    PT17H30M: 'PT17H30M',
    PT18H:    'PT18H',
    PT18H30M: 'PT18H30M',
    PT19H:    'PT19H',
    PT23H:    'PT23H'
  };

  _.forEach(this.tablesForTimesAndDiscounts, (timesAndDiscount: TablesForTimesAndDiscount) => {
        _.forEach(timesAndDiscount.discounts, (discount: DiscountTimeTable) => {
          if (discount.scheduleDiscountId === 0) {
            discount.tables = 6;  //regardless of time - all tables
          }
          if (discount.scheduleDiscountId === 10) {
            if (discount10Times[timesAndDiscount.timeSlot]) {
              discount.tables = 2;
            }
          }
          if (discount.scheduleDiscountId === 20) {
            if (discount20Times[timesAndDiscount.timeSlot]) {
              discount.tables = 2;
            }
          }
        });
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM