Code Snippet: Take a Dataset tag field and convert to comma separated string

There aren’t enough code snippets when I google for things, so I figure I should document this so that whoever comes after me can do it too.

I did this in a dynamic page, hence use of .getCurrentItem()

#dataset = dataset for the page…
aTagField = whatever tag field in the dataset
tempVariable = storing it as a string (no spaces yet) so I can use the .replace on it
finalResult = modified string with spaces

let tempVariable = “” + $w( ‘#dataset’ ).getCurrentItem().aTagField
let finalResult = tempVariable.replace(/,/g, ", " );

Hope this helps someone.

possible to do the other way around? like if i wish to convert a string like [“a”,“b”] to separate tags in the collection? so it’ll show “a” and “b” as a separate field tag

@kn the tags field is an array so you can simplify it to one line using join

let commaSep = $w('#dynamicDataset).getCurrentItem().tags.join(", ")

Shouldn’t it be —> split() if he wants to separate it?

From —> [“Joined, String”]
To ----> [“separated”, “Strings”]

???

Only the showed example of Package-Pals seems not to be correct.
From [“a, b”] —> to —> [“a”, “b”] —> right?

@russian-dima i was responding to OP with a simple one liner to convert an array of strings into a single line comma separated string

IE
[“one”, “two”]

With join will string it to
“one, two”

In regards to Package Pals
Do you mean converting a string of comma separated to an array?

If so then yes. you will want to remove the white spaces first

var str = “one, two, three, four”
var arr = string.replace(/ /g,‘’).split(“,”)

Output would then be [“one”, “two”, “three”, “four”]