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

前端 未结 6 998
孤城傲影
孤城傲影 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:19

    1. Create comma separated string
    2. Reverse it
    3. Replace first occurrence of " ," with " dna " (" and " backwards)
    4. Reverse it again
    [1, 2, 3]
        .join(', ')
        .split('').reverse().join('')
        .replace(' ,', ' dna ')
        .split('').reverse().join('')
    

提交回复
热议问题