Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  129] [ 3]  / answers: 1 / hits: 16170  / 12 Years ago, fri, february 8, 2013, 12:00:00

How can I turn an array, such as [Susy, John, Mary] to Susy, John, and Mary in Javascript? I can't get Ruby's to_sentence method out of my head.


More From » jquery

 Answers
12

Join all items except the last one, and then add that:



var s = arr.slice(0, arr.length - 1).join(', ') + , and  + arr.slice(-1);


Demo: http://jsfiddle.net/JYKcK/1/


[#80360] Wednesday, February 6, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bradleymoisesy

Total Points: 121
Total Questions: 105
Total Answers: 95

Location: Nepal
Member since Mon, Jan 4, 2021
3 Years ago
;