【问题标题】:Javascript autoincrement indexJavascript 自动增量索引
【发布时间】:2015-04-29 02:44:24
【问题描述】:

如果可能的话,我需要创建一个函数或使用一个已经制作的库来自动增加索引。例如,如果它以“A”开头,则必须递增到“Z”,在“Z”之后,它必须从“A1”开始,并且尽快。 . .'B1','C1',...'Z1','A2','B2',...是否存在这样的东西?

我的想法是这样,但是从 'A' 开始,不要添加 number 。 . .

function nextChar(cont,letter) {


if (cont === 0){return letter;}
else {


letter=letter.charCodeAt(0) + 1;

return String.fromCharCode(letter);

   }

}

【问题讨论】:

  • 这只是一个嵌套的for循环,任何必须创建套牌的纸牌游戏都会做类似的事情(纸牌从1到13,当13被击中时,改变花色)。

标签: javascript


【解决方案1】:

许多选项之一:

function nextIndex(idx) {
  var m = idx.match(/^([A-Z])(\d*)$/)
  if(!m)
    return 'A';
  if(m[1] == 'Z')
    return 'A' + (Number(m[2] || 0) + 1);
  return String.fromCharCode(m[1].charCodeAt(0) + 1) + m[2];
}

var a = "";
for(i = 0; i < 100; i++) {
  a = nextIndex(a)
  document.write(a + ", ")
}

【讨论】:

    【解决方案2】:

    这个比georg的效率低,但乍一看可能更容易理解:

    for (var count = 0, countlen = 5; count < countlen; count++) {
        for (var i = 65, l = i + 26; i < l; i++) {
            console.log(String.fromCharCode(i) + (count !== 0 ? count : ''));
        }
    }
    

    DEMO

    【讨论】:

      【解决方案3】:

      请允许我提出一个更面向对象的解决方案:

      function Index(start_with) {
          this.reset = function(reset_to) {
              reset_to = reset_to || 'A';
              this.i = reset_to.length > 1 ? reset_to[1] : 0; // needs more input checking
              this.c = reset_to[0].toUpperCase(); // needs more input checking
              return this;
          };
      
          this.inc = function(steps) {
              steps = steps || 1;
              while(steps--) {
                  if (this.c === 'Z') {
                    this.i++;
                    this.c = 'A';
                  } else {
                    this.c = String.fromCharCode(this.c.charCodeAt(0) + 1);
                  }
              }
              return this;
          };
      
          this.toString = function() {
              if (this.i === 0) return this.c;
              return this.c + '' + this.i;
          };
      
          this.reset(start_with);
      }
      
      var a = new Index(); // A
      console.log('a = ' + a.inc(24).inc().inc()); // Y, Z, A1
      var b = new Index('B8'); // B8
      console.log('a = ' + a.reset('Y').inc()); // Y, Z
      console.log('b = ' + b); // B8
      

      【讨论】:

        【解决方案4】:

        另一种思考方式是,您的“A1”索引只是整数的自定义呈现:0='A',1='B',26='A1'等。

        所以你也可以重载 Number 对象来渲染你的索引。最大的好处是所有的数学运算仍然有效,因为你总是在处理数字:

        Number.prototype.asIndex = function() {
            var n = this;
            var i = Math.floor(n / 26);
            var c = String.fromCharCode('A'.charCodeAt(0) + n % 26);
            return '' + c + (i ? i : '');   
        }
        Number.parseIndex = function(index) {
            var m;
            if (!index) return 0;
            m = index.toUpperCase().match(/^([A-Z])(\d*)$/);
            if (!m || !m[1]) return 0;
            return Number((m[1].charCodeAt(0) - 'A'.charCodeAt(0)) + 26 * (m[2] ? m[2] : 0));
        };
        
        var c = 52;
        var ic = c.asIndex();
        var nc = Number.parseIndex(ic);
        console.log(c+' = '+ic+' = '+nc); // 52 = A2 = 52
        

        如果你这样做,我会先尝试检查新方法是否不存在......

        【讨论】:

          猜你喜欢
          • 2019-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-14
          • 2018-07-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多