# Filter Multi Dimensional Array

**URL:** https://discourse.nodered.org/t/filter-multi-dimensional-array/56334
**Category:** General
**Created:** [9 January 2022 04:09 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334 "2022-01-09T04:09:35Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)
#### Post date: [9 January 2022 04:09 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/1 "2022-01-09T04:09:35Z")

</div>

I can't seem to get the syntax right to only return the array with a "Value" containing "Gaming" in a multi dimensional array.  
This is what i've got so far:

```auto
msg.payload = msg.payload.Volumes.Tags.filter(el => el.Value.includes("Gaming"))

```

My payload returns three "Volumes" and I want to filter out one of the "Volumes"

```auto
{
   "Volumes":[
      {
         "Attachments":[
            {
               "AttachTime":"2021-12-04T18:03:14.000Z",
               "Device":"xvdf",
               "InstanceId":"abcd",
               "State":"attached",
               "VolumeId":"abcd",
               "DeleteOnTermination":false
            }
         ],
         "AvailabilityZone":"ca-central-1a",
         "CreateTime":"2021-12-04T18:03:00.651Z",
         "Encrypted":false,
         "Size":125,
         "SnapshotId":"abcd",
         "State":"in-use",
         "VolumeId":"abcd",
         "Tags":[
            
         ],
         "VolumeType":"sc1",
         "MultiAttachEnabled":false
      },
      {
         "Attachments":[
            {
               "AttachTime":"2022-01-09T01:22:44.000Z",
               "Device":"/dev/sda1",
               "InstanceId":"abcd",
               "State":"attached",
               "VolumeId":"abcd",
               "DeleteOnTermination":false
            }
         ],
         "AvailabilityZone":"ca-central-1a",
         "CreateTime":"2022-01-09T01:22:28.846Z",
         "Encrypted":false,
         "Size":30,
         "SnapshotId":"",
         "State":"in-use",
         "VolumeId":"abcd",
         "Iops":100,
         "Tags":[
            {
               "Key":"Volume",
               "Value":"GamingVolume"
            }
         ],
         "VolumeType":"gp2",
         "MultiAttachEnabled":false
      },
      {
         "Attachments":[
            
         ],
         "AvailabilityZone":"ca-central-1a",
         "CreateTime":"2022-01-09T03:15:01.774Z",
         "Encrypted":false,
         "Size":30,
         "SnapshotId":"",
         "State":"available",
         "VolumeId":"abcd",
         "Iops":100,
         "Tags":[
            {
               "Key":"Snapshot",
               "Value":"Snapshot"
            }
         ],
         "VolumeType":"gp2",
         "MultiAttachEnabled":false
      }
   ]
}

```

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [9 January 2022 04:32 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/2 "2022-01-09T04:32:37Z")

</div>

hello .. you need to loop through `Volumes` and then through its individual `Tags`  
and push the `volume` match to a temporary array (`result`),  
or at least that how my thought process worked it out 😉

```auto
let result = [];

msg.payload.Volumes.forEach(volume => {
    volume.Tags.forEach(tag => {
        if (tag.Value.startsWith("Gaming")) {
            result.push(volume)
        }
    })
})

msg.payload = result;

return msg;

```

**Test Flow:**

```auto
[{"id":"b558a74069d392f3","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"Volumes\":[{ \"Attachments\":[ { \"AttachTime\":\"2021-12-04T18:03:14.000Z\", \"Device\":\"xvdf\", \"InstanceId\":\"abcd\", \"State\":\"attached\", \"VolumeId\":\"abcd\", \"DeleteOnTermination\":false }], \"AvailabilityZone\":\"ca-central-1a\", \"CreateTime\":\"2021-12-04T18:03:00.651Z\", \"Encrypted\":false, \"Size\":125, \"SnapshotId\":\"abcd\", \"State\":\"in-use\", \"VolumeId\":\"abcd\", \"Tags\":[], \"VolumeType\":\"sc1\", \"MultiAttachEnabled\":false }, { \"Attachments\":[{ \"AttachTime\":\"2022-01-09T01:22:44.000Z\", \"Device\":\"/dev/sda1\", \"InstanceId\":\"abcd\", \"State\":\"attached\", \"VolumeId\":\"abcd\", \"DeleteOnTermination\":false }], \"AvailabilityZone\":\"ca-central-1a\", \"CreateTime\":\"2022-01-09T01:22:28.846Z\", \"Encrypted\":false, \"Size\":30, \"SnapshotId\":\"\", \"State\":\"in-use\", \"VolumeId\":\"abcd\", \"Iops\":100, \"Tags\":[{ \"Key\":\"Volume\", \"Value\":\"GamingVolume\" }], \"VolumeType\":\"gp2\", \"MultiAttachEnabled\":false }, { \"Attachments\":[], \"AvailabilityZone\":\"ca-central-1a\", \"CreateTime\":\"2022-01-09T03:15:01.774Z\", \"Encrypted\":false, \"Size\":30, \"SnapshotId\":\"\", \"State\":\"available\", \"VolumeId\":\"abcd\", \"Iops\":100, \"Tags\":[{ \"Key\":\"Snapshot\", \"Value\":\"Snapshot\" }], \"VolumeType\":\"gp2\", \"MultiAttachEnabled\":false } ] }","payloadType":"json","x":360,"y":840,"wires":[["2f027bc423a030ee","ad0d28f5265f8459"]]},{"id":"2f027bc423a030ee","type":"debug","z":"54efb553244c241f","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":480,"y":780,"wires":[]},{"id":"ad0d28f5265f8459","type":"function","z":"54efb553244c241f","name":"","func":"let result = [];\n\nmsg.payload.Volumes.forEach(volume => {\n volume.Tags.forEach(tag => {\n if (tag.Value.startsWith(\"Gaming\")) {\n result.push(volume)\n }\n })\n})\n\nmsg.payload = result;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":840,"wires":[["b44fcbe216010fe7"]]},{"id":"b44fcbe216010fe7","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":840,"wires":[]}]

```

ps. by the way .. when you say .. containing "Gaming" .. the keyword is always at the start ? or could it be anywhere in the string of `Value`. if yes .. then possibly you'll need to change `startsWith` with RegEx or something.

---

<div class="post-metadata">

### Author: ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)
#### Post date: [9 January 2022 04:39 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/3 "2022-01-09T04:39:51Z")

</div>

Thank you very much! I can set the tag values so I can make sure it always starts the same.

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [9 January 2022 04:47 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/4 "2022-01-09T04:47:45Z")

</div>

There could be a problem with the above code.

If you have a Volume that has two or more Tags that have a "Gaming" keyword, you may end up with duplicates of the same Volume in the result.

If this is not what you want then we need to work on the code a bit more.

---

<div class="post-metadata">

### Author: ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)
#### Post date: [9 January 2022 04:48 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/5 "2022-01-09T04:48:49Z")

</div>

Understood. I can create unique tags and update the code as needed.  
Thanks again!

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [9 January 2022 09:12 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/6 "2022-01-09T09:12:57Z")

</div>

> [@JohnnyPicnic](#):
>
> `msg.payload = msg.payload.Volumes.Tags.filter(el => el.Value.includes("Gaming"))`

The reason that fails is that msg.payload.Volumes.Tags is not an array. In terms of the syntax it should have been something like  
`msg.payload = msg.payload.Volumes.filter(el => el.Tags.includes("Gaming"))`

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [9 January 2022 09:36 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/7 "2022-01-09T09:36:00Z")

</div>

> [@Colin](#):
>
> el.Tags.includes("Gaming")

@Colin I think `includes` is not going to find a match in this case because his Tags is an array of objects

```auto
"Tags":[
            {
               "Key":"Volume",
               "Value":"GamingVolume"
            }
         ],

```

if it was an array of strings eg `"Tags":["Gaming","Sports"]` it would ..  
dont know if he can change the way his data is structured

---

<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: [9 January 2022 09:53 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/8 "2022-01-09T09:53:54Z")

</div>

This should work

```auto
msg.payload.Volumes = msg.payload.Volumes.filter(({Tags}) => {
    return Tags.find(obj => obj.Value.startsWith("Gaming"))
    })

return msg;

```

[edit] and should handle multiple tags.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [9 January 2022 10:00 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/9 "2022-01-09T10:00:55Z")

</div>

> [@UnborN](#):
>
> `includes` is not going to find a match in this case because his Tags is an array of objects

Yes, I was just pointing out the problem with the call of filter().

---

<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: [9 January 2022 16:36 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/10 "2022-01-09T16:36:29Z")

</div>

Another option is to use a `change` node with this JSONata expression:

```
payload.Volumes[Tags[Value.$match(/gaming/i)]]
```

---

<div class="post-metadata">

### Author: ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)
#### Post date: [9 January 2022 17:33 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/11 "2022-01-09T17:33:14Z")

</div>

This kind of thing makes my head hurt 😉

---

<div class="post-metadata">

### Author: ![JohnnyPicnic](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/johnnypicnic/32/34787_2.png) [@JohnnyPicnic](https://discourse.nodered.org/u/JohnnyPicnic)
#### Post date: [9 January 2022 17:34 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/12 "2022-01-09T17:34:30Z")

</div>

That's very nice. The volumes are also an array so it works with:

```auto
payload.[Volumes[Tags[Value.$match(/gaming/i)]]]

```

---

<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: [10 January 2022 03:25 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/13 "2022-01-10T03:25:34Z")

</div>

😂 Yeah, it does help if you are "wired" a bit differently than the norm...

... which may be why I also love working with regex and xslt. 😉

---

<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: [24 January 2022 03:26 UTC](https://discourse.nodered.org/t/filter-multi-dimensional-array/56334/14 "2022-01-24T03:26:16Z")

</div>

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