[removed] nice human readable join of list

前端 未结 7 1682
半阙折子戏
半阙折子戏 2021-01-26 10:11

Having a list (array) of tags [\'tag1\', \'tag2\', \'tag3\'] I want to generate a nice title like: Content tagged tag1, tag2 and tag3.

For the

7条回答
  •  再見小時候
    2021-01-26 10:15

    Something like this would be my approach

    function arrayFormat(arr) {
        var output = arr.splice(0, arr.length - 1).join(", ");
        output += " and " + arr[0];
        return output;
    }
    console.log(arrayFormat(["a", "b", "c"]));

提交回复
热议问题