# 优化写法

# 1、switch case 优化

testFunction(key) {
      switch (key) {
        case 1:
          this.test = "第一";
          break;
        case 2:
          this.test = "第二";
          break;
        case 3:
          this.test = "第三";
          break;
        default:
          this.test = "第四";
          break;
      }
      return this.test;
    },

这样是最常见的写法,我们可以利用 ES6 中的 Map 对象进行处理

handlerLogic1(status) {
      const actions = new Map([
        [1, "第一" // 或者是一个函数],
        [2, "第二"],
        [3, "第三"],
        ["default", "第四"]
      ]);
      let action = actions.get(status) || actions.get("default");
      this.test = action;
    }

在 Map 中参数可以是函数,也可以是纯数组。

# 2、数组去重

方法一、

const uniqueWithSpreadOperator = [...new Set(cars)];

方法二、

 Array.prototype.unique = function() {
        const tmp = new Map();
        return this.filter(item => {
          return !tmp.has(item) && tmp.set(item, 1);
        });
      };
      let array = [{ 1: 0 }, { 2: 3 }, { 2: 3 }];
      array.unique();

方法三、

Array.prototype.unique = function() {
    return this.sort().reduce((init, current) => {
        if (init.length === 0 || init[init.length - 1] !== current) {
            init.push(current);
        }
        return init;
    }, []);
}
var array = [1, 2, 3, 43, 45, 1, 2, 2, 4, 5];
array.unique();

方法四、

Array.prototype.unique = function() {
    const newArray = [];
    this.forEach(item => {
        if (!newArray.includes(item)) {
            newArray.push(item);
        }
    });
    return newArray;
}
var array = [1, 2, 3, 43, 45, 1, 2, 2, 4, 5];
array.unique();

# 3、重新组装对象

根据对象的值重新组装对象

 const cities = [
        { name: "Paris", visited: "no" },
        { name: "Lyon", visited: "no" },
        { name: "Marseille", visited: "yes" },
        { name: "Rome", visited: "yes" },
        { name: "Milan", visited: "no" },
        { name: "Palermo", visited: "yes" },
        { name: "Genoa", visited: "yes" },
        { name: "Berlin", visited: "no" },
        { name: "Hamburg", visited: "yes" },
        { name: "New York", visited: "yes" }
      ];

      const result = cities.reduce((accumulator, item) => {
        console.log(accumulator);
        return {
          ...accumulator,
          [item.name]: item.visited
        };
      }, {});

      console.log(result);

有条件的对象属性
优化原因:不需要根据一个条件写两个不同的对象,可根据短路运算符进行判断

 const getUsers = dj => {
        return {
          name: "张三",
          id: 1,
          ...(dj && {
            names: "李四",
            ids: 2
          })
        };
      };
      const s = getUsers(true);
      console.log(s);
      const a = getUsers(false);
      console.log(a);

一个对象解构成多个对象
将一个对象解构成多个对象,有时候一个对象包含很多属性,而我们只需要其中的几个,这里可以使用解构方式来提取我们需要的属性

 const currentRequest = {
        index: 0,
        size: 30,
        systemType: 1,
        indicator: "指标",
        system: "模型",
        version: "明细",
        time: 2019,
        region: "成都"
      };
      let systemdata = {},
        indicatordata = {};
      ({
        index: systemdata.index,
        system: systemdata.system,
        size: systemdata.size,
        ...indicatordata
      } = currentRequest);
      console.log(systemdata);
      console.log(indicatordata);