Search string in array

Hello everyone!
I would like to ask for help below:
There are multiple objects in an array, each object has a "mac-address" variable. I would like to get the values ​​of all the "mac-address" variables on a separate line. The problem with the code below is that it finds the first variable and stops. Look no further. It would also be a good idea to keep looking. (sorry, I still don't know English)

let macaddresses = ["xx:xx:xx:xx:xx"];  // Ide felsorolsz minden klienst

for (var it = 0; it < msg.payload.length; it++) {
    if (macaddresses.includes(msg.payload[it]["mac-address"]))
 {

And this is the array

 [
   {
       ".id":"*3B",
       "interface":"wlan1",
       "mac-address":"xx:xx:xx:xx:xx:xx",
       "ap":"false",
       "wds":"false",
       "bridge":"false",
       "rx-rate":"54Mbps",
       "tx-rate":"65Mbps-20MHz/1S"
   }
      {
       ".id":"*3x",
       "interface":"wlan1",
       "mac-address":"yy:yy:yy:yy:yy:yy",
       "ap":"false",
       "wds":"false",
       "bridge":"false",
       "rx-rate":"54Mbps",
       "tx-rate":"65Mbps-20MHz/1S"
   }
  
]

(You should actually find next)

Try this.

const Filtered = msg.payload.filter((AE) => macaddresses.includes(AE['mac-address']))

Filtered will now be an array, that only contains a matching mac from macaddresses

this is a drive by visit, so might not be correct

I don’t understand why you use a function node and write code instead of using a flow.

I would need the value of all the "mac-address" variables

Could you help with this?

You can use a change node to pull just the list of mac addresses out of the payload.

Select the J: pulldown and enter this JSONata expression:

payload.`mac-address`[]

The output msg.payload will be an array of all the mac addresses, as long as the input payload is an array.

Here is an example of 3 methods, function with Javascript, change with JSONata and a low code option with split and join.
hope it helps

[{"id":"d6cf43f3.1a4c08","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\".id\":\"*3B\",\"interface\":\"wlan1\",\"mac-address\":\"xx:xx:xx:xx:xx:xx\",\"ap\":\"false\",\"wds\":\"false\",\"bridge\":\"false\",\"rx-rate\":\"54Mbps\",\"tx-rate\":\"65Mbps-20MHz/1S\"},{\".id\":\"*3x\",\"interface\":\"wlan1\",\"mac-address\":\"yy:yy:yy:yy:yy:yy\",\"ap\":\"false\",\"wds\":\"false\",\"bridge\":\"false\",\"rx-rate\":\"54Mbps\",\"tx-rate\":\"65Mbps-20MHz/1S\"}]","payloadType":"json","x":160,"y":1960,"wires":[["9cb4a346.0e272","8f21907.5bde07","53a4ea5.a66fb14"]]},{"id":"9cb4a346.0e272","type":"function","z":"bf9e1e33.030598","name":"","func":"msg.payload = msg.payload.map(obj =>  obj[\"mac-address\"])\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":1960,"wires":[["51a7f44d.0d30ac"]]},{"id":"8f21907.5bde07","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[$$.payload.`mac-address`]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":2020,"wires":[["51a7f44d.0d30ac"]]},{"id":"53a4ea5.a66fb14","type":"split","z":"bf9e1e33.030598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":280,"y":2100,"wires":[["ec74879d.e89d18"]]},{"id":"51a7f44d.0d30ac","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":1960,"wires":[]},{"id":"ec74879d.e89d18","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.mac-address","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2120,"wires":[["6a5ec440.d583b4"]]},{"id":"6a5ec440.d583b4","type":"join","z":"bf9e1e33.030598","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":650,"y":2100,"wires":[["51a7f44d.0d30ac"]]}]
3 Likes

And if you want just have a text with MAC in each line - modify the JOIN Node which was posted by @E1cid

[
    {
        "id": "d6cf43f3.1a4c08",
        "type": "inject",
        "z": "4735369af5996aeb",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[{\".id\":\"*3B\",\"interface\":\"wlan1\",\"mac-address\":\"xx:xx:xx:xx:xx:xx\",\"ap\":\"false\",\"wds\":\"false\",\"bridge\":\"false\",\"rx-rate\":\"54Mbps\",\"tx-rate\":\"65Mbps-20MHz/1S\"},{\".id\":\"*3x\",\"interface\":\"wlan1\",\"mac-address\":\"yy:yy:yy:yy:yy:yy\",\"ap\":\"false\",\"wds\":\"false\",\"bridge\":\"false\",\"rx-rate\":\"54Mbps\",\"tx-rate\":\"65Mbps-20MHz/1S\"}]",
        "payloadType": "json",
        "x": 330,
        "y": 580,
        "wires": [
            [
                "53a4ea5.a66fb14"
            ]
        ]
    },
    {
        "id": "53a4ea5.a66fb14",
        "type": "split",
        "z": "4735369af5996aeb",
        "name": "",
        "splt": "\\n",
        "spltType": "str",
        "arraySplt": 1,
        "arraySpltType": "len",
        "stream": false,
        "addname": "",
        "x": 470,
        "y": 580,
        "wires": [
            [
                "ec74879d.e89d18"
            ]
        ]
    },
    {
        "id": "51a7f44d.0d30ac",
        "type": "debug",
        "z": "4735369af5996aeb",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 990,
        "y": 580,
        "wires": []
    },
    {
        "id": "ec74879d.e89d18",
        "type": "change",
        "z": "4735369af5996aeb",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.mac-address",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 650,
        "y": 580,
        "wires": [
            [
                "6a5ec440.d583b4"
            ]
        ]
    },
    {
        "id": "6a5ec440.d583b4",
        "type": "join",
        "z": "4735369af5996aeb",
        "name": "",
        "mode": "custom",
        "build": "string",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 830,
        "y": 580,
        "wires": [
            [
                "51a7f44d.0d30ac"
            ]
        ]
    }
]

If you want to filter for mac addresses just add a switch node before the JOIN node.

Can this be for a cycle?

Sorry do not understand the question. Can you define this and cycle please.

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