有没有办法连接js数组中的元素,但让最后一个分隔符不同?

Eiv*_*eth 34 javascript

我想要的是类似的东西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)

  • 对于任何想要在不使用牛津逗号的情况下使用此功能的人,请将区域设置更改为“en-GB” (8认同)

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)

  • 请注意,这假定`a.length> 1`.你应该覆盖`a.length <= 1` (4认同)

Jos*_*eph 37

我可以建议:

['tom', 'dick', 'harry'].join(', ').replace(/, ([^,]*)$/, ' and $1')
> "tom, dick and harry"
Run Code Online (Sandbox Code Playgroud)

  • 这很好用,但缺点是其他开发人员很难阅读,除非上面有注释。权衡。 (3认同)
  • 如果您可以确定数组元素不包含逗号,那么这是一个干净的解决方案。如果他们这样做,它将无法正常工作。例如:“['英格兰,英国','伯尔尼,瑞士','亚洲']`将翻译为“英格兰和英国,伯尔尼和瑞士和亚洲” (3认同)
  • regex解决方案非常适合该问题,并且具有能够支持0、1或多个数组元素的优点。好答案! (2认同)

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)


Jus*_*ier 10

不,这很具体,你必须编写一个自定义函数.好消息是,正如你所说,一旦你Array.join用来照顾所有的分隔符,最后一个很容易更新.


Cha*_*ynn 6

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)


Yai*_*evy 5

紧凑版:)

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)