Help

Re: Insert commas and word "and" into a list from a multiple select field?

669 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Sam_Oliker-Frie
4 - Data Explorer
4 - Data Explorer

I’m trying to set up an automated email that fills in the contents of a multiple select field. Is there a way to insert the multiple select field in something like a plain english list?

For example, I have a multiple select that has [Sam; Suzy; Pete]. I would like to insert the sentence “Sam, Suzy, and Pete” into my email.

Thanks!

1 Reply 1
Bill_Felix
6 - Interface Innovator
6 - Interface Innovator

Here is an implementation in the Scripting App:

// 🧲
const list = input.config().any_list
//console.log(list)

// 🧞‍♂️
function makeSentence(list) {
    switch(list.length){
        case 1: 
            //console.log(list[0])
            output.set('sentence', list[0]);
        break;
        case 2: 
            let two = list
                    .join(' and ')
            //console.log(two)
            output.set('sentence', two);
    break;
    default :
            let more = list.length ? list
                    .slice(0, list.length - 1)
                    .join(', ')
                    + ', and ' + list.slice(-1)
                : console.error('The list was totally empty!')
                //console.log(more)
                output.set('sentence', more)
    }
}

// ▶
makeSentence(list)

// someday this will be the answer:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat
//

Let me know if that works for you as it is something that I am going to need very shortly in my own base. Thanks!