# Array filter not working

**URL:** <https://discourse.nodered.org/t/array-filter-not-working/48758>\
**Category:** General\
**Created:** [22 July 2021 08:20 UTC](https://discourse.nodered.org/t/array-filter-not-working/48758 "2021-07-22T08:20:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![metter](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@metter](https://discourse.nodered.org/u/metter)\
**Post date:** [22 July 2021 08:20 UTC](https://discourse.nodered.org/t/array-filter-not-working/48758/1 "2021-07-22T08:20:38Z")

</div>

I have this node red test setup where I want to filter an array of objects with another array. The problem is, the test setup returns an unfiltered copy of the original array.

could anyone tell me where I go wrong?

the input array `payload` is an array of elements:  
`msg.payload = [{"altname":"melon"},{"altname":"cherry"},{"altname":"banana"}]`

the filter array `exclude` has 2 elements:  
`msg.exclude = [["melon"],["banana"]]`

the function node is:

```auto
msg.payload = msg.payload.filter(el => !msg.exclude.includes(el.altname));
node.warn(msg.payload);
return msg;

```

I expect the output array to have only 1 element:  
`[{"altname":"cherry"}]`

my setup:

```auto
[{"id":"b4b2e91f.8e4368","type":"inject","z":"42edc0b9.7ba91","name":"","props":[{"p":"payload"},{"p":"exclude","v":"[[\"melon\"],[\"banana\"]]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"altname\":\"melon\"},{\"altname\":\"cherry\"},{\"altname\":\"banana\"}]","payloadType":"json","x":1650,"y":320,"wires":[["3fbdbc18.951c94"]]},{"id":"3fbdbc18.951c94","type":"function","z":"42edc0b9.7ba91","name":"cycle counter","func":"\n\nmsg.payload = msg.payload.filter(el => !msg.exclude.includes(el.altname));\n\nnode.warn(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1810,"y":320,"wires":[["ab67374c.c64458"]]},{"id":"ab67374c.c64458","type":"debug","z":"42edc0b9.7ba91","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2040,"y":300,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 July 2021 08:22 UTC](https://discourse.nodered.org/t/array-filter-not-working/48758/2 "2021-07-22T08:22:59Z")

</div>

> [@metter](#):
>
> msg.exclude = [["melon"],["banana"]]

Don't put the strings in sub arrays.

---

<div class="post-metadata">

**Author:** ![metter](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@metter](https://discourse.nodered.org/u/metter)\
**Post date:** [22 July 2021 08:50 UTC](https://discourse.nodered.org/t/array-filter-not-working/48758/3 "2021-07-22T08:50:18Z")

</div>

I see!  
Now the next problem: The data is provided as `msg.exclude = [["melon"],["banana"]]`  
how would turn this to: `msg.exclude = ["melon","banana"]`?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 July 2021 08:54 UTC](https://discourse.nodered.org/t/array-filter-not-working/48758/4 "2021-07-22T08:54:37Z")

</div>

Look at array `map` function to convert it

```auto
const a2 = a1.map(e=>e[0]); 

```

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [22 July 2021 14:51 UTC](https://discourse.nodered.org/t/array-filter-not-working/48758/5 "2021-07-22T14:51:34Z")

</div>

Also please don't cross post the same question to here and Stack Overflow

> <https://stackoverflow.com/questions/68480882/node-red-filter-array-with-array/68481654#68481654>

---

<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:** [5 August 2021 14:52 UTC](https://discourse.nodered.org/t/array-filter-not-working/48758/6 "2021-08-05T14:52:29Z")

</div>

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