Function evaluatExpressions converts array to object

Hi

A time ago I got help with this workflow, now I realise that it is converting an array (instances) to and object by mistake, I have tried everything to solve it but I can’t find a solution. It might have to do with that I do something wrong.

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "boolean": [],
          "string": [
            {
              "name": "topicid",
              "value": "radars_in_use2"
            }
          ]
        },
        "options": {}
      },
      "name": "setTopicId",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        300,
        330
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "state",
              "value": "running"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        460,
        340
      ]
    },
    {
      "parameters": {
        "functionCode": "const data = [\n{\n  \"octopic\": \"radars_in_use1\",\n    \"contextData\": {\n      \"equipmentObject\": {\n        \"class\": \"navigation\",\n        \"id\": {\n          \"idTagScheme\": \"vis\",\n          \"vis\": {\n            \"function\": \"723.11\",\n            \"name\": \"Radar systems\"\n          },\n          \"otherIdTags\": {\n            \"sfi\": {\n              \"code\": \"411\",\n              \"name\": \"RADAR PLANTS\"\n            },\n            \"jp_mach\": \"\",\n            \"ovd\": \"\",\n            \"companySerial\": \"\"\n          }\n        },\n        \"instances\": [\n          {\n            \"id\": {\n              \"vis\": {\n                \"component\": \"N91\",\n                \"instanceId\": \"0\",\n                \"id\": \"723.11+N91\",\n                \"name\": \"Radar X-band\"\n              }\n            },\n            \"state\": \"{{$node['Set'].json['state']}}\",\n            \"otherIdTags\": {\n              \"sfi\": {\n                \"id\": \"411.001\",\n                \"name\": \"Radar X-band\"\n              }\n            }\n          },\n          {\n            \"id\": {\n              \"vis\": {\n                \"component\": \"N92\",\n                \"instanceId\": \"0\",\n                \"id\": \"723.11+N92\",\n                \"name\": \"Radar S-band\"\n              }\n            },\n            \"state\": \"{{$node['Set'].json['state']}}\",\n            \"otherIdTags\": {\n              \"sfi\": {\n                \"id\": \"411.002\",\n                \"name\": \"Radar S-band\"\n              }              \n            }\n          }\n        ]\n      }\n    }\n  },\n  {\n  \"octopic\": \"radars_in_use2\",\n    \"contextData\": {\n      \"equipmentObject\": {\n        \"class\": \"navigation\",\n        \"id\": {\n          \"idTagScheme\": \"vis\",\n          \"vis\": {\n            \"function\": \"723.11\",\n            \"name\": \"Radar systems\"\n          },\n          \"otherIdTags\": {\n            \"sfi\": {\n              \"code\": \"411\",\n              \"name\": \"RADAR PLANTS\"\n            },\n            \"jp_mach\": \"\",\n            \"ovd\": \"\",\n            \"companySerial\": \"\"\n          }\n        },\n        \"instances\": [\n          {\n            \"id\": {\n              \"vis\": {\n                \"component\": \"N91\",\n                \"instanceId\": \"0\",\n                \"id\": \"723.11+N91\",\n                \"name\": \"Radar X-band\"\n              }\n            },\n            \"state\": \"Running\",\n            \"otherIdTags\": {\n              \"sfi\": {\n                \"id\": \"411.001\",\n                \"name\": \"Radar X-band\"\n              }\n            }\n          },\n          {\n            \"id\": {\n              \"vis\": {\n                \"component\": \"N92\",\n                \"instanceId\": \"0\",\n                \"id\": \"723.11+N92\",\n                \"name\": \"Radar S-band\"\n              }\n            },\n            \"state\": \"Running\",\n            \"otherIdTags\": {\n              \"sfi\": {\n                \"id\": \"411.002\",\n                \"name\": \"Radar S-band\"\n              }              \n            }\n          }\n        ]\n      }\n    }\n  }\n\n\n];\n\nconst eventId = $node[\"setTopicId\"].json[\"topicid\"]\nconst items = data.filter(entry => {\n    let isValid = true;\n    isValid = entry.octopic === eventId ? isValid : false;\n    return isValid;\n});\n\nconst newItems = [];\nitems.forEach(item => newItems.push({\n    json: item\n}));\nreturn newItems;\n"
      },
      "name": "inEvent",
      "type": "n8n-nodes-base.function",
      "position": [
        630,
        320
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "\nfunction resolveData(data) {\n  if (data !== null && typeof data === 'object') {\n    const returnData = {};\n    for (const key of Object.keys(data)) {\n    console.log('key: ' + key);\n      if (key === '_id') {\n        returnData[key] = data[key].toString();\n        continue;\n      }\n      returnData[key] = resolveData(data[key]);\n    }\n    return returnData;\n  }\n\n  if (typeof data === 'string' && data.charAt(0) === '{') {\n    return $evaluateExpression(data);\n  } \n  \n  return data;\n}\n\nreturn [\n  {\n    json: resolveData($node[\"inEvent\"].json),\n  }\n]\n\n"
      },
      "name": "Convert5",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        860,
        450
      ]
    }
  ],
  "connections": {
    "setTopicId": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "inEvent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "inEvent": {
      "main": [
        [
          {
            "node": "Convert5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Yes, it was not written to handle arrays.

Here the workflow with the adjusted Function-Node to also support arrays:

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "boolean": [],
          "string": [
            {
              "name": "topicid",
              "value": "radars_in_use2"
            }
          ]
        },
        "options": {}
      },
      "name": "setTopicId",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        850,
        350
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "state",
              "value": "running"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1000,
        350
      ]
    },
    {
      "parameters": {
        "functionCode": "const data = [\n{\n  \"octopic\": \"radars_in_use1\",\n    \"contextData\": {\n      \"equipmentObject\": {\n        \"class\": \"navigation\",\n        \"id\": {\n          \"idTagScheme\": \"vis\",\n          \"vis\": {\n            \"function\": \"723.11\",\n            \"name\": \"Radar systems\"\n          },\n          \"otherIdTags\": {\n            \"sfi\": {\n              \"code\": \"411\",\n              \"name\": \"RADAR PLANTS\"\n            },\n            \"jp_mach\": \"\",\n            \"ovd\": \"\",\n            \"companySerial\": \"\"\n          }\n        },\n        \"instances\": [\n          {\n            \"id\": {\n              \"vis\": {\n                \"component\": \"N91\",\n                \"instanceId\": \"0\",\n                \"id\": \"723.11+N91\",\n                \"name\": \"Radar X-band\"\n              }\n            },\n            \"state\": \"{{$node['Set'].json['state']}}\",\n            \"otherIdTags\": {\n              \"sfi\": {\n                \"id\": \"411.001\",\n                \"name\": \"Radar X-band\"\n              }\n            }\n          },\n          {\n            \"id\": {\n              \"vis\": {\n                \"component\": \"N92\",\n                \"instanceId\": \"0\",\n                \"id\": \"723.11+N92\",\n                \"name\": \"Radar S-band\"\n              }\n            },\n            \"state\": \"{{$node['Set'].json['state']}}\",\n            \"otherIdTags\": {\n              \"sfi\": {\n                \"id\": \"411.002\",\n                \"name\": \"Radar S-band\"\n              }              \n            }\n          }\n        ]\n      }\n    }\n  },\n  {\n  \"octopic\": \"radars_in_use2\",\n    \"contextData\": {\n      \"equipmentObject\": {\n        \"class\": \"navigation\",\n        \"id\": {\n          \"idTagScheme\": \"vis\",\n          \"vis\": {\n            \"function\": \"723.11\",\n            \"name\": \"Radar systems\"\n          },\n          \"otherIdTags\": {\n            \"sfi\": {\n              \"code\": \"411\",\n              \"name\": \"RADAR PLANTS\"\n            },\n            \"jp_mach\": \"\",\n            \"ovd\": \"\",\n            \"companySerial\": \"\"\n          }\n        },\n        \"instances\": [\n          {\n            \"id\": {\n              \"vis\": {\n                \"component\": \"N91\",\n                \"instanceId\": \"0\",\n                \"id\": \"723.11+N91\",\n                \"name\": \"Radar X-band\"\n              }\n            },\n            \"state\": \"Running\",\n            \"otherIdTags\": {\n              \"sfi\": {\n                \"id\": \"411.001\",\n                \"name\": \"Radar X-band\"\n              }\n            }\n          },\n          {\n            \"id\": {\n              \"vis\": {\n                \"component\": \"N92\",\n                \"instanceId\": \"0\",\n                \"id\": \"723.11+N92\",\n                \"name\": \"Radar S-band\"\n              }\n            },\n            \"state\": \"Running\",\n            \"otherIdTags\": {\n              \"sfi\": {\n                \"id\": \"411.002\",\n                \"name\": \"Radar S-band\"\n              }              \n            }\n          }\n        ]\n      }\n    }\n  }\n\n\n];\n\nconst eventId = $node[\"setTopicId\"].json[\"topicid\"]\nconst items = data.filter(entry => {\n    let isValid = true;\n    isValid = entry.octopic === eventId ? isValid : false;\n    return isValid;\n});\n\nconst newItems = [];\nitems.forEach(item => newItems.push({\n    json: item\n}));\nreturn newItems;\n"
      },
      "name": "inEvent",
      "type": "n8n-nodes-base.function",
      "position": [
        1150,
        350
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "\nfunction resolveData(data) {\n  if (data === null) {\n    return data;\n  }\n  if (Array.isArray(data)) {\n    return data.map(item => resolveData(item));\n  } else if (typeof data === 'object') {\n    const returnData = {};\n    for (const key of Object.keys(data)) {\n    console.log('key: ' + key);\n      if (key === '_id') {\n        returnData[key] = data[key].toString();\n        continue;\n      }\n      returnData[key] = resolveData(data[key]);\n    }\n    return returnData;\n  }\n\n  if (typeof data === 'string' && data.charAt(0) === '{') {\n    return $evaluateExpression(data);\n  } \n  \n  return data;\n}\n\nreturn [\n  {\n    json: resolveData($node[\"inEvent\"].json),\n  }\n]\n\n"
      },
      "name": "Convert5",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1300,
        350
      ]
    }
  ],
  "connections": {
    "setTopicId": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "inEvent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "inEvent": {
      "main": [
        [
          {
            "node": "Convert5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}