Turn the array into a grammatically correct sentence with a comma

If I have an array in Javascript that looks like

searchComponents = ['element1', 'element2', 'element3']; 

What is the necessary logic to turn this into a sentence like:

"element1, element2 and element3"

Similarly, if there are only two elements, it should look like this:

"element1 and element2"

and so on and so forth. I am stuck.

+12
javascript string arrays
source share
8 answers

One simple solution:

 function arrayToSentence (arr) { var last = arr.pop(); return arr.join(', ') + ' and ' + last; } console.log(arrayToSentence(['one','two','three'])); 

JS Fiddle demo .

And a little more complicated / funny solution (because who doesnโ€™t like stupid, sometimes ...):

 function arrayToSentence (arr) { var len = arr.length; return arr.reduce(function(a,b,c){ return a + (c - 1 === length ? ', ' : ' and ') + b; }); } console.log(arrayToSentence(['one','two','three'])); 

JS Fiddle demo .

Literature:

+19
source share
 function toSentence(arr) { return arr.slice(0, -2).join(', ') + (arr.slice(0, -2).length ? ', ' : '') + arr.slice(-2).join(' and '); } 

Using

 toSentence([1]) 

one

 toSentence([1, 2]) 

1 and 2

 toSentence([1, 2, 3]) 

1, 2 and 3

 toSentence([1, 2, 3, 4, 5, 6]) 

1, 2, 3, 4, 5 and 6

+17
source share

Try my composite library:

https://github.com/adamshaylor/compound-subject

It includes controls over which character to delimit (for example, with commas or semicolons), as well as limit all items (that is, the โ€œOxford commaโ€).

+2
source share

More ES6 / 7 version without mutation:

 function buildSentence (arr = []) { if (arr.length === 0) { return ''; } if (arr.length === 1) { return arr[0]; } const newArr = [...arr]; const last = newArr.pop(); return newArr.join(', ') + ' and ' + last; } buildSentence([]); buildSentence(['a']); buildSentence(['a', 'b']); buildSentence(['a', 'b', 'c']); 
+2
source share

Here is one insert:

 const arrayToSentence = (a) => [a.slice(0, -1).join(', '), a.pop()].filter(w => w !== '').join(' and '); console.log(arrayToSentence(['foo', 'bar', 'baz'])); console.log(arrayToSentence(['foo', 'bar'])); console.log(arrayToSentence(['foo'])); console.log(arrayToSentence([])); 
+2
source share

Like this...

 function parseArray(arr) { var s=arr.toString(); var c=s.lastIndexOf(","); if(c!=-1) s=(s.substr(0,c)+" and "+s.substr(c+1)).replace(/,/g,", "); return s[0].toUpperCase()+s.substr(1)+"."; } console.log(parseArray(["one", "two","three","four","five"])); console.log(parseArray(["one", "two"])); console.log(parseArray(["one"])); 

Exit:

 One, two, three, four and five. One and two. One. 

Grammatically correct?

0
source share

Here is a typed version that uses lodash features:

 import { concat, first, join, last, slice } from 'lodash' function joinOxford (list: string[]): string { switch (list.length) { case 1: return first(list) case 1: return '${first(list)} and ${last(list)}' default: return join(concat(slice(list, 0, -1), ['and ${last(list)}']), ', ') } } 

Using:

 joinOxford([foo]) // => foo joinOxford([foo, bar]) // => foo and bar joinOxford([foo, bar, baz]) // => foo, bar, and baz joinOxford([foo, bar, baz, qux, quux, quuz]) // => foo, bar, baz, qux, quux, and quuz 
0
source share

Late answer, but this is my attempt. Relatively simple, immutable arguments both work for 0-N elements.

 const toListSentence = (arr) => arr.length < 3 ? arr.join(' and ') : '${arr.slice(0, -1).join(', ')}, and ${arr[arr.length - 1]}'; console.log(toListSentence([])); console.log(toListSentence(['apple'])); console.log(toListSentence(['apple', 'banana'])); console.log(toListSentence(['apple', 'banana', 'peach'])); 

0
source share

All Articles