Select item starting with

Hello,

With Expressions, is there a way to select the item out of an array beginning with a certain word(s) ?

Sadly not. That could theoretically be done with .filter() but that does not work in expressions (seems to be some kind of parsing issue). For that treason would you have to do that in a Function-Node instead.

Hmm, You got an example for that maybe?

It would look like this:

items[0].json.testArray.filter(item => item.key === 'key1')[0].value

Here an example workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      testArray: [\n        {\n          key: 'key1',\n          value: 'value1',\n        },\n        {\n          key: 'key2',\n          value: 'value2',\n        }\n      ]\n    }\n  }\n];"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json.myVariable = items[0].json.testArray.filter(item => item.key === 'key1')[0].value;\nreturn items;"
      },
      "name": "Get Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Get Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

And that would theoretically select the key with the value starting with “key1” doesnt matter what comes after it?

Not sure what you mean exactly. It would have to be called exactly “key1”, if it is called “key1_asdf” it for example not select it.

So from my HTML Extract i get this response:

I dynamically want to select the one beginning with “Description Work” since the position of this line is dynamic

They you would have to use something like this instead:

items[0].json.testArray.filter(item => item.key.startsWith('Description Work'))[0].value
TypeError: Cannot read property 'filter' of undefined

Then the value before filter does not exist. So you have to fix it by replacing it with the correct one.

Doesnt matter which value i change it to, it doesnt match

You seem to have an array of strings. So it would then be the following:

items[0].json.data.find(item => item.startsWith('Description Work'));

The output is every single string for some reason

That is not possible. It has to work with the data you did send me via private message.

An here an example workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      data: [\n        'do not match',\n        'Description Work: match this'\n      ]\n    }\n  }\n];"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json.myVariable = items[0].json.data.find(item => item.startsWith('Description Work'));\nreturn items;"
      },
      "name": "Get Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Get Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

And here a screenshot how the node looks like after it got executed:

Strangely enough now it randomly works, Quick question: What does it mean when a node turns grey?

If a node is grey it means it got deactivated. That means it will not do anything anymore and will simply be skipped.

That happens if you either select it and press “d” or if you click on the “Pause” button above it.

Allright thank you, I guess i sometimes misclick