在基于项目索引的情况下,追加序数后缀('-st','-nd','-rd','-th')。

3

假设我有一个选项值的数组,就像这样:

var arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

我需要将其翻译为格式化字符串的数组,例如:

var result = ['1-st option is "a"', '2-nd option is "b", '3-rd option is "c"', '4-th option is "d"',...];

我已经用以下方式完成了大部分工作:

我用以下方式完成了大部分工作:

var result = [];
for(var i = 0; i < arr.length; i++){
    result.push((i+1)+' option is "'+arr[i]+'"');
}

它生成字符串,比如1 option is "a"等等。但我似乎无法处理那些后缀('-st','-nd','-rd','-th')。你们能帮我解决这个问题吗?谢谢!
4个回答

2
你可以将必要的后缀放入数组中,并选择与你的索引相对应的后缀:

您可以将必要的后缀放入数组中,并选择与您的索引相对应的后缀:

最初的回答

const arr = [...'abcdefghijklmnopqrstuvwxyz'];
const suffixes = ['th', 'st', 'nd', 'rd'];

const result = arr.map((item, i) => 
  (idx = ~~(i % 10) + 1 > 3 || ~~(i / 10) == 1 ? 0 : ~~(i % 10) + 1, 
  `${i+1}-${suffixes[idx]} options is '${item}'`));

console.log(result);
.as-console-wrapper {min-width: 100%}


在一个超过100个项目的数组中测试过了。很好的答案。 - Doug Watkins
我接受你的答案,因为它是第一个提供所需输出的。现在@Ghoul Ahmed已经编辑了他的帖子,它看起来也紧凑高效,我本可以点赞的,但我的声望记录还不足以获得这个特权:( - nwaitland513
@U25lYWt5IEJhc3RhcmQg,你的解决方案是最快的,所以我点赞了它 :) - Ghoul Ahmed

2
尝试使用以下代码,它有效: https://en.wikipedia.org/wiki/Ordinal_indicator#English -st 用于以1结尾的数字(例如1st,发音为first)
-nd 用于以2结尾的数字(例如92nd,发音为ninety-second)
-rd 用于以3结尾的数字(例如33rd,发音为thirty-third)
作为上述规则的例外,所有以11、12或13结尾的“teen”数字都使用-th(例如11th,发音为eleventh,112th,发音为one hundred [and] twelfth)
-th 用于所有其他数字(例如9th,发音为ninth)。

var arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

function addSuffix(i) {
    var j = i % 10,
        k = i % 100;
    if (j == 1 && k != 11) {
        return i + "-st";
    }
    if (j == 2 && k != 12) {
        return i + "-nd";
    }
    if (j == 3 && k != 13) {
        return i + "-rd";
    }
    return i + "-th";
}

var result = [];
for (var i = 0; i < arr.length; i++) {
    result.push(`${addSuffix(i + 1)} option is  '${arr[i]}'`);
}
console.log(result);


请描述一下这段代码是如何回答问题的,以帮助其他人理解这个答案。 - FZs

1

try this:

const  arr= ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

const ordinal_suffix_of = (n) => (["st","nd","rd"][((n+90)%100-10)%10-1] || "th")

const result  = arr.map((res,i)=> `${i+1}-${ordinal_suffix_of(i+1)} option is '${res}'`);

console.log(result);


2
所有你的结果都是“-st”。 - apple apple
1
这并没有回答问题。请阅读一下,OP正在寻求什么。 - FZs
1
@GhoulAhmed:我喜欢你目前的帖子编辑,所以我点赞了,尽管它似乎比我的有点 ;) - Yevhen Horbunkov

1
您可以使用取模运算符%创建一个函数来处理这个问题,例如:
function calc_suffix(number) {
    let rest = number % 10;

    if(rest === 1 && number != 11) { return 'st'; }
    else if(rest === 2 && number != 12) { return 'nd'; }
    else if(rest === 3 && number != 13) { return 'rd'; }
    else { return 'th'; }
}

最终结果将如下所示:

var result = [];
for(var i = 0; i < arr.length; i++){
    result.push((i+1) + "-" + calc_suffix(i+1) + " option is "'+arr[i]+'"');
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接