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.
Any way to handle in JQuery?
解决方案
You can use .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 with and if there are at least two elements.
最后
以上就是缓慢橘子最近收集整理的关于jquery 逗号分割截取字符串_数组到逗号分隔的字符串和最后一个标记在jquery中使用'和'而不是逗号...的全部内容,更多相关jquery内容请搜索靠谱客的其他文章。
发表评论 取消回复