If I have an array in Javascript that looks like
searchComponents = ['element1', 'element2', 'element3'];
What is the necessary logic to turn it into a sentence like:
"element1, element2, and element3"
Likewise, if there are only two elements it should read like:
"element1 and element2"
and so on and so forth. I am stuck.
One easy solution:
function arrayToSentence (arr) {
var last = arr.pop();
return arr.join(', ') + ' and ' + last;
}
console.log(arrayToSentence(['one','two','three']));
And a slightly more complex/ridiculous solution (because who doesn't like silly, occasionally...):
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']));
References:
function toSentence(arr) {
return arr.slice(0, -2).join(', ') +
(arr.slice(0, -2).length ? ', ' : '') +
arr.slice(-2).join(' and ');
}
usage
toSentence([1])
1
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
Try my compound-subject library:
https://github.com/adamshaylor/compound-subject
It includes controls over what character to delimit with (eg commas versus semicolons) as well as whether to delimit all the subjects (ie the “Oxford comma”).
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']);
Here's a one liner:
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([]));
How's 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"]));
Output is:
One, two, three, four and five.
One and two.
One.
Grammatically correct?
Here's a typed version that utilizes lodash functions:
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)}`]), ', ')
}
}
Usage:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.