How do I filter the elements of an array and output a specific value in a new property of the parent item?

Hey!
How to create a new property “col_task” in the current item, in which to output the value from the array element according to the condition (see screenshot)?

Hey @Roket,

You can refer to the below workflow

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [{\n  json:{\n    \"key\": \"col_1\",\n    prop: [\n      {\n        \"key\":\"asdf\",\n        \"col_task\": \"Something\"\n      },\n      {\n        \"key\":\"col_1\",\n        \"col_task\": \"Data important\"\n      }\n    ]\n  }\n  },{json:{\n    \"key\": \"col_2\",\n    prop: [\n      {\n        \"key\":\"col_2\",\n        \"col_task\": \"Something\"\n      },\n      {\n        \"key\":\"col_1\",\n        \"col_task\": \"Data important\"\n      }\n    ]\n  }\n}]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "\nfor (item of items) {\n  const key = item.json.key;\n  var col_task;\n  for (i of item.json.prop){\n    if (i.key == key) {\n      item.json.col_task = i.col_task;\n    }\n  }\n  \n}\n\nreturn items;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
3 Likes

@harshil1712, its works great!

1 Like