记录vue和js操作——尽管很快实现了功能,可总感觉到不爽,特别想打人

  需求产生的原因是:后端有一些数据是从旧平台直接迁移过来的,新平台需要根据迁移过来的数据,自动生产新的数据格式。

  操作符有如下几种,分项、支路和数字配合操作符可以自定义组合,例如 [000000000001]+<00001>,带[号的表示type是branch,带<表示type是sub。

 // 操作符列表
    operatorList: [
        '+',
        '-',
        '*',
        '/',
        '(',
        ')',
        'inputNumber'
      ],

  举例:[000000000001]+<00001>

   解析为:
   [{"type":"branch","name":"1#","value":92501,"electricAddr":"000000000001"},{"type":"+"},{"type":"sub","name":"电电电","value":69640,"number":"00001"}]

  部分实现代码如下:

  data () {
    return {
      // 操作符列表
      operatorList: [
        '+',
        '-',
        '*',
        '/',
        '(',
        ')',
        'inputNumber'
      ],
      filterOperator: [], //操作符过滤和排序列表
}
method:{
    //操作符排序和过滤
    geOoperatorFilter (expStr) {
      let arr = this.operatorList.map(n => {
        return { index: expStr.indexOf(n), val: n };
      });
      arr = arr.filter(f => f.index > -1);
      arr.sort(this.sortId);
      this.filterOperator = arr.map(m => {
        return m.val
      });
      // console.log('this.filterOperator :>> ', this.filterOperator);
    },
    sortId (a, b) {
      return a.index - b.index;//由低到高
    },
    // true:数值型的,false:非数值型
    myIsNaN (value) {
      return typeof value === 'number' && !isNaN(value);
    },
    //[000000000001]+[11101110011]-<1002>*<c3110>
    reveserExpress (expStr) {
      this.geOoperatorFilter(expStr);
      let arr = [];
      this.analysisData(arr, expStr);
      // console.log('arr :>> ', arr);
      let jsonArr = [];
      arr.forEach(n => {
        if (n.includes('[')) {
          jsonArr.push({ "type": "branch", "name": "", "value": '', "electricAddr": n.replace('[', '').replace(']', '') });
        }
        if (n.includes('<')) {
          jsonArr.push({ "type": "sub", "name": "", "value": '', "number": n.replace('<', '').replace('>', '') });
        }
        if (this.filterOperator.includes(n)) {
          jsonArr.push({ "type": n });
        }
        //如果是个数字
        if (this.myIsNaN(n)) {
          jsonArr.push({ "type": 'number', "value": n });
        }
      });
      // console.log('jsonArr :>> ', jsonArr);
      this.form.expressionJson = JSON.stringify(jsonArr);
    },
    //是否包含分隔符
    containOperator (word) {
      return this.filterOperator.some(f => word.includes(f));
    },
    analysisData (arr, expStr) {
      // debugger;
      this.filterOperator.forEach(n => {
        if (expStr.includes(n)) {
          // debugger;
          let tempArr = expStr.split(n);
          tempArr.forEach((t, index) => {
            //不包含分隔符
            if (!this.containOperator(t) && !arr.includes(t)) {
              // debugger;
              arr.push(t);
              if (index < tempArr.length - 1) {
                arr.push(n);
              }
            } else {
              // debugger;
              this.analysisData(arr, t);
            }
          })
        }
      })
    },

}

  测试:

 let expStr = '[000000000001]-[11101110011]/<1002>+<c3110>';
   this.reveserExpress(expStr);

  运行结果:

[{"type":"branch","name":"","value":"","electricAddr":"000000000001"},{"type":"-"},{"type":"branch","name":"","value":"","electricAddr":"11101110011"},
{"type":"/"},{"type":"sub","name":"","value":"","number":"1002"},{"type":"+"},{"type":"sub","name":"","value":"","number":"c3110"}]

  虽然很快实现了功能,可是写这样的代码总感觉好恶心,可又不知道该怎么办?好想打人,可是没有发泄对象,治好扇自己一巴掌!

  众里寻他千百度,猛一回头,卧艹,那人却在一个人过马路~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章