How to Use filter() Function in Set Node?

How can I use the filter() function in the Set node to remove duplicate records from a variable?

The filter function works as expected, but when I try to use it in the Set node, I receive null instead of the expected result.

{{ $json["message"].match(/host=([^ ]+)/)[1].replace(/[<>]/g, '').split('|').filter((value, index, self) => {
  return self.indexOf(value) === index;
}) }}

Information on your n8n setup

  • n8n version: Version 0.225.2
  • Database (default: SQLite): MySQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main): own
  • Running n8n via (Docker, npm, n8n cloud, desktop app): : docker
  • Operating system: Ubuntu 22.04.2 LTS

Could you add a sample of the input JSON upon which you are attempting to evaluate this expression? Also please include the result type (string? array?) and value you expect as output from the expression.

Based on your match and replace calls, I guessed at the following input item JSON, and the expression works fine.

{
    "message": "host=<1|<2>|4|4|6|6|7|7|<8>|9|11|11>"
}

Hey @Asuwini_P You can use {{ $json.array.unique() }} for that.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.