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.
12 Answers
Using newest JavaScript (Chrome 72+):
new Intl.ListFormat().format(["Susy", "John", "Mary"]);
which will result in:
"Susy, John, and Mary"
2 Comments
Petercopter
Whoa, this is going to be great, when it's supported! developers.google.com/web/updates/2018/12/intl-listformat
morgler
Awesome! But unfortunately still poorly supported by other browsers :( caniuse.com/#search=ListFormat
Join all items except the last one, and then add that:
var s = arr.slice(0, arr.length - 1).join(', ') + ", and " + arr.slice(-1);
7 Comments
Popnoodles
Just one note- Americans put a comma before the "and". OP has done this.
Guffa
@popnoodles: Ah, then you would just use
+ ", and " +.Alex
I can see a couple of problems with this method. If there's only 1 element in the array, it will return ", and Susy". For two elements, it returns "Susy, and John", which isn't grammatically correct.
Guffa
@Alex: Yes, with less that three items you need additional code, but that's outside the scope of the question. There is no answer here that handles that case.
Alex
Good point, but I think if you're only concerned with the specific use case presented, you could end up with something like jAndy's (joke) answer :-) I've posted an answer that covers the most obvious cases I could think of.
|
There are a few extra cases in this one:
- Do we want to have a comma before the "and" (Oxford Comma)
- Either way, we don't want a comma if there are only 2 elements
- If there is only 1 element, we just want that word returned
I think this fits all of those use cases:
function joinSentence( array, oxford_comma ){
if( array.length > 1 ){
var lastWord = " and " + array.pop();
if( oxford_comma && array.length > 1 ){
lastWord = "," + lastWord;
}
}else{
var lastWord = "";
}
return array.join(", ") + lastWord;
}
Comments
And another solution:
(function(a) {
var b = (a||(a=[])).pop();
return (b ? a.length ? [a.join(", "),b] : [b] : a).join(" and ");
})(["Susy", "John", "Mary"]);
or just defining your ruby-like sentence method in array's prototype:
Array.prototype.sentence = function(comma,and) {
var b = this.pop();
return (b ? this.length ? [this.join(comma||", "),b] : [b] : this).join(and||" and ");
};
["Susy", "John", "Mary"].sentence();
//"Susy, John and Mary"
["Susy", "John", "Mary"].sentence(" and ", " as well as ");
//"Susy and John as well as Mary"
["Susy", "John", "Mary", "Helmut"].sentence();
//"Susy, John, Mary and Helmut"
Comments
modify the last then join
var source = ["Susy", "John", "Mary"];
source.push( 'and ' +source.pop());
console.log(source.join(', '))// Susy, John, and Mary
You can use .join() and prepend the last value with an "and"
var array = ['susy', 'john', 'mary'];
array[array.length-1] = 'and '+array[array.length-1];
//string is "susy, john, and mary"
var string = array.join(', ');
var arr = ["Susy", "John", "Mary"];
var str = arr.join(", ");
1 Comment
pjmorse
This produces
"Susy, John, Mary", which is not the desired result.
["Susy", "John", "Mary"].join(',').split(/,M/).join(' and M');