如何用lodash改善这个程序?

时间:2017-02-04 19:17:04

标签: javascript arrays typescript lodash

我已经编写了一些我并不感到自豪的代码,作为一个lodash noob我会很感激有关如何改进此代码的一些反馈。

要求是为某些类型的折扣填充一系列具有默认值的时段,例如:enter image description here

例如,左侧每个时间段的每个折扣(顶行)有6个表。在中午12点,20%折扣列中有2个表。在右侧是每个时段的总数。

我目前拥有此格式的数据:

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

然后我做了一个foreach循环下来的时间段然后foreach时间段,另一个foreach循环遍历所有折扣。然后,如果折扣与X匹配,我检查该时间和折扣是否在另一个数组(discount20Times)中,如果是,则执行某些操作。

代码有效,但我确信它效率低下 - 我只是不知道采用Lodash改进它的方法是什么?

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

1 个答案:

答案 0 :(得分:0)

你可以通过使用一个对象而不是一个数组来立即加速查找(类型声明可能是错误的,但你明白了)

我缩短了其中一个变量名称,以便于阅读

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