How to filter items from json?

Hi my dear friends

Can I ask how can I filter some items from json and then return new json?

In fact I use below nodes but is don’t work.

{
  "nodes": [
{
  "parameters": {
    "functionCode": "return [\n  {\n    json: {\n      myNumber: 0\n    }\n  },\n  {\n    json: {\n      myNumber: 1\n    }\n  },\n  {\n    json: {\n      myNumber: 2\n    }\n  }\n];"
  },
  "name": "Mock-Data",
  "type": "n8n-nodes-base.function",
  "typeVersion": 1,
  "position": [
    1230,
    820
  ]
},
{
  "parameters": {
    "functionCode": "const result = items.filter(function(item){\n    return item.myNumber == 1;         \n});\nreturn result;"
  },
  "name": "filter",
  "type": "n8n-nodes-base.function",
  "typeVersion": 1,
  "position": [
    1390,
    820
  ]
}
  ],
  "connections": {
"Mock-Data": {
  "main": [
    [
      {
        "node": "filter",
        "type": "main",
        "index": 0
      }
    ]
  ]
}
  }
}

Can anyone help me please?

Best regards;

Solved :sweat_smile:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      myNumber: 0\n    }\n  },\n  {\n    json: {\n      myNumber: 1\n    }\n  },\n  {\n    json: {\n      myNumber: 2\n    }\n  }\n];"
      },
      "name": "Mock-Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1230,
        820
      ]
    },
    {
      "parameters": {
        "functionCode": "return items.filter(item => {\n  return item.json.myNumber === 1\n})\n"
      },
      "name": "filter",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1390,
        820
      ]
    }
  ],
  "connections": {
    "Mock-Data": {
      "main": [
        [
          {
            "node": "filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like