I have an array of strings and I would like to display them as a comma separated string but add "and" for the last element. For example I have
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join(' ,');
outputs
'Banana, Orange, Apple, Mango'
Is there any way the I add "and" for the last word so it outputs
'Banana, Orange, Apple and Mango'
I have an array of strings and I would like to display them as a comma separated string but add "and" for the last element. For example I have
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join(' ,');
outputs
'Banana, Orange, Apple, Mango'
Is there any way the I add "and" for the last word so it outputs
'Banana, Orange, Apple and Mango'
Share
Improve this question
asked Oct 30, 2016 at 12:18
snowflakes74snowflakes74
1,3071 gold badge20 silver badges48 bronze badges
1
|
5 Answers
Reset to default 9You may instead use Array.reduce
function and implement your own logic, for example:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.reduce(function (p, d, i) {
return p + (i === fruits.length - 1 ? ' and ' : ', ') + d;
});
You can join the first 3 elements of the array using slice and join then append the last element concatenated with 'and'.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.slice(0, fruits.length - 1).join(', ');
energy += ' and ' + fruits[fruits.length - 1];
You can't pass a function to .join
, unfortunately, so you do need to iterate through the loop and join it yourself:
const array = ['banana', 'apple', 'taco']
let result = array[0]
for (let i = 1; i < array.length; i++) {
if (i < array.length - 1) {
result += ', ' + array[i]
} else {
result += ' and ' + array[i]
}
}
console.log(result)
You could make a function for this, though:
// General purpose custom/dynamic join function creator.
function dynamicJoin(fn) {
return function(array) {
let result = array[0]
for (let i = 1; i < array.length; i++) {
const joiner = fn(array[i - 1], array[i], i, array)
result += joiner + array[i]
}
return result
}
}
const joinCommaOrAnd = dynamicJoin(function(a, b, i, arr) {
return (i === arr.length - 1) ? ' and ' : ', '
})
console.log(joinCommaOrAnd(['bread', 'fish', 'butter']))
Function toSentence of underscore.string has the exact functionality you want. If you don't want to get whole library, you could just borrow toSentence
:
function toSentence(array, separator, lastSeparator, serial) {
separator = separator || ', ';
lastSeparator = lastSeparator || ' and ';
var a = array.slice(),
lastMember = a.pop();
if (array.length > 2 && serial) lastSeparator = separator + lastSeparator;
return a.length ? a.join(separator) + lastSeparator + lastMember : lastMember;
};
You can try this,
var delimiter = ', ';
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join(delimiter);
var index = energy.lastIndexOf(delimiter);
energy = energy.substring(0, index) + ' and ' + energy.substring(index + delimiter.length);
console.log(energy);
.reduce
or replace the last comma in the string. – VLAZ Commented Oct 30, 2016 at 12:24