javascript 数组到逗号分隔的字符串,最后一个标签在 jquery 中使用“和”而不是逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16251822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array to Comma separated string and for last tag use the 'and' instead of comma in jquery
提问by Suleman Ahmad
I have checked many questions and answers regarding join array with comma separated string, But my problem is that, I am making the string readable for human i.e I have tags array that's if their is two tags, then it would be tag1 and tag2
and if their is 100 tags then it would be tag1, tag2, ,,,tag99 and tag100
for last one use the and
and before using the comma as a separator.
我已经检查了许多关于用逗号分隔的字符串连接数组的问题和答案,但我的问题是,我正在使字符串对人类可读,即我有标签数组,如果它们是两个标签,那么它会是tag1 and tag2
,如果它们是 100标签那么tag1, tag2, ,,,tag99 and tag100
最后一个使用and
和 之前使用逗号作为分隔符。
Any way to handle in JQuery?
有什么办法可以在 JQuery 中处理吗?
回答by Blender
You can use .slice()
:
您可以使用.slice()
:
> var a = [1, 2, 3, 4, 5];
> [a.slice(0, -1).join(', '), a.slice(-1)[0]].join(a.length < 2 ? '' : ' and ');
'1, 2, 3, 4 and 5'
a.slice(0, -1).join(', ')
: takes all but the last element and joins them together with a comma.a.slice(-1)[0]
: it's the last element..join(a.length < 2 ? '' : ' and ')
: joins that string and the last element withand
if there are at least two elements.
a.slice(0, -1).join(', ')
: 取除最后一个元素之外的所有元素,并用逗号将它们连接在一起。a.slice(-1)[0]
: 这是最后一个元素。.join(a.length < 2 ? '' : ' and ')
:and
如果至少有两个元素,则将该字符串和最后一个元素连接起来。
回答by cipher
Another quick solution is to replace the last comma with and.
另一个快速解决方案是用 and 替换最后一个逗号。
The following code:
以下代码:
var a = [0,1,2,3,4];
a.join(', ').replace(/,(?!.*,)/gmi, ' and');
should do the trick. (Also, play with the regex modifiers to your need).
应该做的伎俩。(另外,根据您的需要使用正则表达式修饰符)。
One probable issue might be of time when you have large array, but it should work.
当您拥有大型阵列时,一个可能的问题可能是时间问题,但它应该可以工作。
回答by Khadim Ali
Try this:
试试这个:
var substr = new Array("One", "Two", "Three");
var commaList = '';
var i;
for (i = 0; i < substr.length; ++i) {
if (i == substr.length - 1)
commaList += " and " + substr[i];
else
commaList += ", " + substr[i];
}
console.debug(commaList.substr(2, commaList.length));
回答by Gras Double
(note: you may skip to the end to find the final ready-to-pick code)
(注意:您可以跳到最后找到最终的可挑选代码)
The one-liner from Blender's answerworks great, though I'd favor something more understandable:
从一个班轮Blender的答案的伟大工程,但我会赞成的东西更容易理解:
function arrayToText(a) {
if (a.length === 0) {
return '';
} else if (a.length === 1) {
return String(a[0]); // cast to string, for consistency with the other cases
} else if (a.length === 2) {
return a.join(' and ');
} else {
return a.slice(0, -1).join(', ') + ' and ' + a[a.length-1];
}
}
The same using a switch, see how nicer it is to read:
同样使用开关,看看它有多好读:
function arrayToText(a) {
switch (a.length) {
case 0:
return '';
case 1:
return String(a[0]); // cast to string, for consistency with the other cases
case 2:
return a.join(' and ');
default:
return a.slice(0, -1).join(', ') + ' and ' + a[a.length-1];
}
}
Of course, these codes can be shortened/optimized, for example by merging the "0", "1" and "2" cases into just the a.join(' and ')
from the case "2". I let you tweak these codes according to your taste :)
当然,这些代码可以被缩短/优化,例如通过将“0”、“1”和“2”案例合并到“2”案例中a.join(' and ')
。我让你根据自己的喜好调整这些代码:)
Finally, here is my end result, I think it has a great balance between understandability, shortness and efficiency:
最后,这是我的最终结果,我认为它在可理解性、简短性和效率之间取得了很好的平衡:
function arrayToText(a) {
if (a.length <= 2) {
return a.join(' and ');
} else {
return a.slice(0, -1).join(', ') + ' and ' + a[a.length-1];
}
}
回答by Jaspreet Singh
Able to solve it using reduce as well:
也可以使用 reduce 来解决它:
let items = [1, 2, 3, 4, 5];
items.reduce((acc, curr, i) =>
i !== 0 ? acc + `${items.length - 1 === i ? ` and ` : ', '}` + curr : curr, '');
// "1, 2, 3, 4 and 5"
回答by ArneHugo
- Create comma separated string
- Reverse it
- Replace first occurrence of " ," with " dna " (" and " backwards)
- Reverse it again
- 创建逗号分隔的字符串
- 反转它
- 将第一次出现的“,”替换为“dna”(“和”向后)
- 再反转
[1, 2, 3]
.join(', ')
.split('').reverse().join('')
.replace(' ,', ' dna ')
.split('').reverse().join('')