Is there a way to join the elements in an js array, but let the last separator be different?

前端 未结 11 787
灰色年华
灰色年华 2021-02-03 17:40

What I want is something like Array.join(separator), but which takes a second argument Array.join(separator, beforeLastElement), so when I say [f

11条回答
  •  一整个雨季
    2021-02-03 18:14

    There's no predefined function, because it's quite simple.

    var a = ['a', 'b', 'c'];
    var str = a.slice(0, -1).join(',')+' or '+a.slice(-1);
    

    There's also a specification problem for the main use case of such a function which is natural language formatting. For example if we were to use the Oxford comma logic we would have a different result than what you're looking for:

    // make a list in the Oxford comma style (eg "a, b, c, and d")
    // Examples with conjunction "and":
    // ["a"] -> "a"
    // ["a", "b"] -> "a and b"
    // ["a", "b", "c"] -> "a, b, and c"
    exports.oxford = function(arr, conjunction, ifempty){
        let l = arr.length;
        if (!l) return ifempty;
        if (l<2) return arr[0];
        if (l<3) return arr.join(` ${conjunction} `);
        arr = arr.slice();
        arr[l-1] = `${conjunction} ${arr[l-1]}`;
        return arr.join(", ");
    }
    

    So it seems better to let this problem in userland.

提交回复
热议问题