Array to Comma separated string and for last tag use the 'and' instead of comma in jquery

前端 未结 6 997
孤城傲影
孤城傲影 2020-12-05 00:55

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 tag

6条回答
  •  时光说笑
    2020-12-05 01:22

    Try this:

        var substr = new Array("One", "Two", "Three");
        var commaList = '';
    
        var i;
        for (i = 0; i < substr.length; ++i) {
            if (i == substr.length - 1)
                commaList += " and " + substr[i];
            else
                commaList += ", " + substr[i];
        }
    
        console.debug(commaList.substr(2, commaList.length));
    

提交回复
热议问题