【问题标题】:How to change counter variable from numbers to letters?如何将计数器变量从数字更改为字母?
【发布时间】:2019-08-29 02:48:00
【问题描述】:

如何将计数器变量从数字更改为字母?假设我有五个部分,每个部分都读取“Section A”,“Section B”,依此类推......我想更改它们的 href 属性,因为它们从“section1”映射到“sectionA”,“section2”到“B节”等?

var sectionNumberLink = 0;
var sectionLetters = assessmentSections.map(function (section) {
    sectionNumberLink++;
    return  '<div class="assess-sec-link">' + '<a href="section' + 
        sectionNumberLink + '">' + "Section" + '</a>' + '</div>';  
}).join('');

【问题讨论】:

  • String.fromCharCode

标签: javascript jquery html


【解决方案1】:

您可以使用String.fromCharCode()String.prototype.charCodeAt() 将数字转换为字母

示例:
警告:如果您有超过 26 个部分,这将失败

function toLetter(number) {
  let base = 'A'.charCodeAt(0);
  return String.fromCharCode(base - 1 + number);
}

console.log(toLetter(1)); // A
console.log(toLetter(2)); // B

如果您需要超过 26 个部分,则需要更多代码:

function toLetter(num) {
  let a = "A".charCodeAt(0);
  let result = '';
  for (let base = 1, mod = 26; (num -= base) >= 0;base = mod, mod *= 26) {
    result = String.fromCharCode(num % mod / base + a) + result;
  }
  return result;
}

console.log(toLetter(1)); // A
console.log(toLetter(27)); // AA

在您的代码 sn-p 中,您可以这样使用它:

let sectionLetters = assessmentSections.map((section, idx) => {
    return  `
      <div class="assess-sec-link">
        <a href="section${toLetter(idx + 1)}">Section</a>
      </div>`;  
}).join('');

【讨论】:

  • 太棒了。感谢您提供所有信息!
【解决方案2】:

.map()方法中可以使用index计算字母表,

//sample
var assessmentSections = ["section1", "section2", "section3"]

var sectionLetters = assessmentSections.map(function (section, index) {
     
    return  '<div class="assess-sec-link">' + '<a href="section' + 
        String.fromCharCode('A'.charCodeAt(0) - 1 + (index+1)) + '">' + "Section" + '</a>' + '</div>';  
    }).join('');
    
console.log(sectionLetters)

【讨论】:

    【解决方案3】:

    你可以这样做:

    const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    //As @georg suggested, you could do it like: 
    for(let c=1;c<=alphabet.length;c++){
      console.log("Section "+alphabet[c-1])
    }

    因此,您可以致电 alphabet[number-1] 将 NUMBER 更改为 CHARACTER。

    记住数组索引从 0 开始,这就是为什么它需要是 number-1

    【讨论】:

    • 我就是这样做的,但是你可以简单地使用alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'"Section" + alphabet[n-1]。不需要辅助函数。
    【解决方案4】:

    我会尝试构建我的 HTML,以便可以使用 &lt;ol type="A"&gt; 而不是 &lt;div&gt;s,这样我就可以自动使用大写字母自动排序,而无需进行索引到字母的计算:

    // create dummy data
    var assessmentSections = Array.from({ length: 50 }, ( item, i ) => {
      return { url: i + 1 };
    });
    var sections = assessmentSections.map(( section, i ) => {
      return `<li class="assess-sec-link"><a href="${ section.url }">Section ${ i + 1 }</a></li>`;
    });
    document.querySelector( 'ol' ).innerHTML = sections.join( '' );
    &lt;ol type="A"&gt;&lt;/ol&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多