How to convert an array of values to #hashtags

Describe the issue/error/question

Hi all, I have an array of comma-separated values and I’m trying to convert them to hashtags. Essentially I’m trying to add the # symbol before each and remove the spaces. Any idea if there is an easy way to accomplish this? The values come in like: “value one, value two” and I’m trying to transform them with a function to “#valueone, #valuetwo”. I tried this to at least get the hashtags but I keep getting an error:

const tags = '{{$json["categories"].toString()}}';
const result = `#${tags.replace(/,/g, '#')}`;
return result;

What is the error message (if any)?

ERROR: Always an Array of items has to be returned! [Line 100]

Information on your n8n setup

  • n8n version: Latest
  • Database you’re using (default: SQLite): SQLite
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker

Assuming the data comes as { data: "value one, value two" } the function below should do it.

const data = items[0].json.data
return [{ json: { data: data.split(',').map((element) => `#${element.trim()}`).join(',') }}]
Example workflow

AWESOME! It works!

How much more difficult would it be to remove all spaces and lowercase all items?

const data = items[0].json.data
return [{ json: { data: data.split(',').map((element) => `#${element.trim().toLowerCase().replace(' ', '')}`).join(',') }}]
1 Like

That worked perfectly! Thanks so much @RicardoE105 !! Truly appreciate your time!

Great it worked. Have fun.