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
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -440,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      data: 'value one, value two'\n    }\n  }\n]\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -160,
        200
      ],
      "notesInFlow": true,
      "notes": "Mockup input"
    },
    {
      "parameters": {
        "functionCode": "const data = items[0].json.data\nreturn [{ json: { data: data.split(',').map((element) => `#${element.trim()}`).join(',') }}]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        140,
        200
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

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.