Best answer

List punctuation with line-item to text in Formatter


Userlevel 1

Can anyone help me out with how to use Formatter to take a line-item list, turn it to text AND use appropriate list punctuation for a sentence.  Example

  1. apple
  2. pear
  3. plumb
  4. cherry

becomes “apple, pear, plumb and cherry” instead of “apple, pear, plumb, cherry”  ?

icon

Best answer by ikbelkirasan 6 April 2020, 02:28

Hi @thurfirhawat - Instead of the formatter, you could use a code step with the following code in it:

const fruits = inputData.fruits.split(",").map((fruit) => fruit.trim());
let text = "";
fruits.forEach((fruit, index) => {
if (index < fruits.length - 1) {
if (index < fruits.length - 2) {
text += `${fruit}, `;
} else {
text += `${fruit} and `;
}
} else {
text += fruit;
}
});

output = [
{
text
}
]

 

 

 

View original

This topic has been closed for comments

3 replies

Userlevel 7
Badge +10

Hi @thurfirhawat 

Question - will the list *always* have 4 items in it?

Userlevel 1

No.  The items are variable.  There might be 0 to ?. @AndrewJDavison_Luhhu 

Userlevel 7
Badge +11

Hi @thurfirhawat - Instead of the formatter, you could use a code step with the following code in it:

const fruits = inputData.fruits.split(",").map((fruit) => fruit.trim());
let text = "";
fruits.forEach((fruit, index) => {
if (index < fruits.length - 1) {
if (index < fruits.length - 2) {
text += `${fruit}, `;
} else {
text += `${fruit} and `;
}
} else {
text += fruit;
}
});

output = [
{
text
}
]