# Search string in array

**URL:** <https://discourse.nodered.org/t/search-string-in-array/62969>\
**Category:** General\
**Tags:** function-node\
**Created:** [23 May 2022 13:50 UTC](https://discourse.nodered.org/t/search-string-in-array/62969 "2022-05-23T13:50:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![azxo1](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@azxo1](https://discourse.nodered.org/u/azxo1)\
**Post date:** [23 May 2022 13:50 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/1 "2022-05-23T13:50:48Z")

</div>

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)

```auto
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

```auto
 [
   {
       ".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)

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [23 May 2022 14:40 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/2 "2022-05-23T14:40:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [23 May 2022 14:40 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/3 "2022-05-23T14:40:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![azxo1](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@azxo1](https://discourse.nodered.org/u/azxo1)\
**Post date:** [23 May 2022 14:48 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/4 "2022-05-23T14:48:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![azxo1](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@azxo1](https://discourse.nodered.org/u/azxo1)\
**Post date:** [23 May 2022 14:48 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/5 "2022-05-23T14:48:48Z")

</div>

Could you help with this?

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [23 May 2022 14:56 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/6 "2022-05-23T14:56:01Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 May 2022 15:04 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/7 "2022-05-23T15:04:28Z")

</div>

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

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [23 May 2022 16:06 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/8 "2022-05-23T16:06:37Z")

</div>

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

```auto
[
    {
        "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"
            ]
        ]
    }
]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dcf076003bb09038686ff55ee380306291b24f58.png)

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

---

<div class="post-metadata">

**Author:** ![azxo1](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@azxo1](https://discourse.nodered.org/u/azxo1)\
**Post date:** [23 May 2022 17:49 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/10 "2022-05-23T17:49:32Z")

</div>

Can this be for a cycle?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 May 2022 17:54 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/11 "2022-05-23T17:54:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 June 2022 09:48 UTC](https://discourse.nodered.org/t/search-string-in-array/62969/12 "2022-06-10T09:48:02Z")

</div>

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