我想要的是类似的东西Array.join(separator),但它需要第二个参数Array.join(separator, beforeLastElement),所以当我说[foo, bar, baz].join(", ", " or")我会得到"foo, bar or baz".我想我可以编写一个用来Array.slice分离最后一个元素的函数,但是有一些我可以使用的众所周知的方法吗?
Pie*_*nry 52
2021 年更新答案!
如果目标是在倒数第二个和最后一个元素之间使用不同的分隔符(例如“and”或“or”),则可以使用Intl.ListFormat
它正是这样做的,并且您可以免费获得 i18n。
除 IE11 之外的所有主要浏览器均支持它。
例子:
const vehicles = ['Motorcycle', 'Bus', 'Car'];
const formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
console.log(formatter.format(vehicles));
// expected output: "Motorcycle, Bus, and Car"
const formatter2 = new Intl.ListFormat('de', { style: 'short', type: 'disjunction' });
console.log(formatter2.format(vehicles));
// expected output: "Motorcycle, Bus oder Car"Run Code Online (Sandbox Code Playgroud)
Den*_*ret 48
你确定应该为此预先定义一个功能吗?
var a = ['a', 'b', 'c'];
var str = a.slice(0, -1).join(',')+' or '+a.slice(-1);
Run Code Online (Sandbox Code Playgroud)
Jos*_*eph 37
我可以建议:
['tom', 'dick', 'harry'].join(', ').replace(/, ([^,]*)$/, ' and $1')
> "tom, dick and harry"
Run Code Online (Sandbox Code Playgroud)
CBa*_*arr 15
建立@ dystroy的答案:
function formatArray(arr){
var outStr = "";
if (arr.length === 1) {
outStr = arr[0];
} else if (arr.length === 2) {
//joins all with "and" but no commas
//example: "bob and sam"
outStr = arr.join(' and ');
} else if (arr.length > 2) {
//joins all with commas, but last one gets ", and" (oxford comma!)
//example: "bob, joe, and sam"
outStr = arr.slice(0, -1).join(', ') + ', and ' + arr.slice(-1);
}
return outStr;
}
Run Code Online (Sandbox Code Playgroud)
示例用法:
formatArray([]); //""
formatArray(["a"]); //"a"
formatArray(["a","b"]); //"a and b"
formatArray(["a","b","c"]); //"a, b, and c"
formatArray(["a","b","c","d"]); //"a, b, c, and d"
Run Code Online (Sandbox Code Playgroud)
Max*_*ier 12
使用 reduce 的在线解决方案:
[1, 2, 3, 4, 5].reduce((text, value, i, array) => text + (i < array.length - 1 ? ', ' : ' or ') + value);
=> "1, 2, 3, 4 or 5"
Run Code Online (Sandbox Code Playgroud)
Array.prototype.join2 = function(all, last) {
var arr = this.slice(); //make a copy so we don't mess with the original
var lastItem = arr.splice(-1); //strip out the last element
arr = arr.length ? [arr.join(all)] : []; //make an array with the non-last elements joined with our 'all' string, or make an empty array
arr.push(lastItem); //add last item back so we should have ["some string with first stuff split by 'all'", last item]; or we'll just have [lastItem] if there was only one item, or we'll have [] if there was nothing in the original array
return arr.join(last); //now we join the array with 'last'
}
> [1,2,3,4].join2(', ', ' and ');
>> "1, 2, 3 and 4"
Run Code Online (Sandbox Code Playgroud)
紧凑版:)
function customJoin(arr,s1,s2){
return(arr.slice(0,-1).join(s1).concat(arr.length > 1 ? s2 : '', arr.slice(-1)));
}
Run Code Online (Sandbox Code Playgroud)
function customJoin(arr,s1,s2){
return(arr.slice(0,-1).join(s1).concat(arr.length > 1 ? s2 : '', arr.slice(-1)));
}
Run Code Online (Sandbox Code Playgroud)