相邻的数字用-串1起来,其他的单独拎出来

  private m(k) {
    // k = [103, 106, 108, 109, 110, 111, 112, 113, 115, 116, 117, 118, 220, 222, 224, 226];
    k = k.map(Number).sort(function (a, b) { return a - b });
    this.DeepDailyList = [];
    if (k.length == 0)
      return;
    var first = 0;
    var old = 0;
    var now = 0;
    for (var i = 0; i < k.length; i++) {
      if (i == 0) {
        first = k[i];
        old = k[i];
        now = k[i];
        if (k.length > 1) {
          if (first + 1 == k[i + 1]) {
          } else {
            this.DeepDailyList.push(first);
          }
        } else {
          this.DeepDailyList.push(first);
        }
      } else {
        now = k[i];
        if (now == old + 1) {
          old = now;
          if (i == k.length - 1) {
            if (old != first) {
              this.DeepDailyList.push(first + "-" + old);
            }
          }
          continue;
        } else {
          if (old != first) {
            this.DeepDailyList.push(first + "-" + old);
          }
          first = k[i];
          old = k[i];
          now = k[i];
          if (i < k.length) {
            if (first + 1 == k[i + 1]) {
            } else {
              this.DeepDailyList.push(first);
            }
          }
        }
      }
    }
    console.log(this.DeepDailyList);
    this.DailyList = this.DeepDailyList.join(",");
    console.log(this.DailyList);
  }

  

posted @ 2021-01-12 19:35  龙旋风  阅读(131)  评论(2编辑  收藏  举报