# Test a property of an array of objects

**URL:** <https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565>\
**Category:** General\
**Created:** [13 March 2021 07:15 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565 "2021-03-13T07:15:21Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [13 March 2021 07:15 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/1 "2021-03-13T07:15:21Z")

</div>

I have an array of objects.

I want to test a specific property of each object. If one test fails, the whole array fails and I want a single 'failed' output.

I can do this in a Javascript function but cannot work out how to do it using nodes.

I can split the array and test each element but that gives me multiple 'fails'.

Suggestions?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [13 March 2021 07:53 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/2 "2021-03-13T07:53:13Z")

</div>

Take array length before splitting and store to flow variable.  
Do validation, pass through only valid results, then join.  
Compare array length with flow variable. If they are equal, result is valid.

```auto
[{"id":"2ee6ff33.d66e7","type":"inject","z":"f56ccabd.7567e8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":580,"wires":[["eb4ab40e.b2b0f8"]]},{"id":"eb4ab40e.b2b0f8","type":"function","z":"f56ccabd.7567e8","name":"correct","func":"var arr = []\nfor(var i=0;i<5;i++){\n var o = {id:i,param:i*2}\n arr.push(o)\n}\nmsg.payload = arr\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":580,"wires":[["fc218f58.360fe"]]},{"id":"74302180.a966b","type":"function","z":"f56ccabd.7567e8","name":"wrong","func":"var arr = []\nfor(var i=0;i<5;i++){\n var o = {id:i,param:i*2}\n arr.push(o)\n}\narr[3].param = \"wrong\"\nmsg.payload = arr\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":620,"wires":[["fc218f58.360fe"]]},{"id":"e0d529fa.3ff2b8","type":"inject","z":"f56ccabd.7567e8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":620,"wires":[["74302180.a966b"]]},{"id":"f4e29903.4ea1d8","type":"split","z":"f56ccabd.7567e8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":370,"y":480,"wires":[["740f558f.abcd3c"]]},{"id":"740f558f.abcd3c","type":"switch","z":"f56ccabd.7567e8","name":"","property":"payload.param","propertyType":"msg","rules":[{"t":"istype","v":"number","vt":"number"},{"t":"lt","v":"10","vt":"num"}],"checkall":"true","repair":true,"outputs":2,"x":490,"y":480,"wires":[[],["e02e217.58589e","fd7038f8.dc51e8"]]},{"id":"fd7038f8.dc51e8","type":"join","z":"f56ccabd.7567e8","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":610,"y":440,"wires":[["a968089e.314e58"]]},{"id":"447c4784.e6cb68","type":"debug","z":"f56ccabd.7567e8","name":"CORRECT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":440,"wires":[]},{"id":"fc218f58.360fe","type":"change","z":"f56ccabd.7567e8","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"$count(payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":220,"y":480,"wires":[["f4e29903.4ea1d8"]]},{"id":"a968089e.314e58","type":"switch","z":"f56ccabd.7567e8","name":"","property":"$count(payload)\t","propertyType":"jsonata","rules":[{"t":"eq","v":"count","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":730,"y":480,"wires":[["447c4784.e6cb68"],["2a788523.1638ca"]]},{"id":"2a788523.1638ca","type":"debug","z":"f56ccabd.7567e8","name":"WRONG","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":880,"y":520,"wires":[]},{"id":"e02e217.58589e","type":"debug","z":"f56ccabd.7567e8","name":"SECOND","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":560,"wires":[]},{"id":"6de4570e.8cbf18","type":"function","z":"f56ccabd.7567e8","name":"wrong","func":"var arr = []\nfor(var i=0;i<5;i++){\n var o = {id:i,param:i*2}\n arr.push(o)\n}\narr[3].param = 12\narr[2].param = \"wrong type\"\nmsg.payload = arr\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":680,"wires":[["fc218f58.360fe"]]},{"id":"5337a04c.2e57b","type":"inject","z":"f56ccabd.7567e8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":680,"wires":[["6de4570e.8cbf18"]]}]

```

EDIT: for multiple condition check, take result from last output and be sure, the checking all rules option is in use.

Trick is to use the sequence recreation, so the joining can be done.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a28c354fb8796243ae7ff46b9c640efa94e1320b.png)

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [13 March 2021 09:34 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/3 "2021-03-13T09:34:04Z")

</div>

Thanks.

Am I right that you can only check one property of an object at one time? Multiple properties (an AND) is multiple switch nodes?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [13 March 2021 09:43 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/4 "2021-03-13T09:43:02Z")

</div>

I think so. For multiple properties and more complex validation I'd preferred to do it with function node.

---

<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:** [13 March 2021 10:00 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/5 "2021-03-13T10:00:22Z")

</div>

You could us JSONata expression in switch node to test multiple properties

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 March 2021 10:06 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/6 "2021-03-13T10:06:25Z")

</div>

@E1cid Interesting you suggest this, I just tried it and although - in the switch - using jsonata - the test option works, it doesn't work. I'm about to open a thread on this very issue with an example.

I'll update this thread with a link as soon as I do.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 March 2021 10:17 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/7 "2021-03-13T10:17:35Z")

</div>

CORRECTION It is possible!! You use the JSONata exp  
 ![Screen Shot 2021-03-13 at 5.15.38 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/474e96a51608c0580b37539eae7b74ac575000b0.png)

Here is a test flow showing it in action:

 ![Screen Shot 2021-03-13 at 5.17.02 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/e/5eeb09a0183a7dbb7224283c6a0a19152ab6eee5.png)

```auto
[{"id":"bcbbf688.15a69","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"7c49307d.9db75","type":"inject","z":"bcbbf688.15a69","name":"dog - brown","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"animal\":\"dog\",\"color\":\"brown\"}","payloadType":"json","x":110,"y":80,"wires":[["322078ba.9084b","a0f00437.ba40d"]]},{"id":"be457815.099aa","type":"inject","z":"bcbbf688.15a69","name":"dog - black","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"animal\":\"dog\",\"color\":\"black\"}","payloadType":"json","x":110,"y":140,"wires":[["322078ba.9084b","a0f00437.ba40d"]]},{"id":"6b0b4f8f.932e18","type":"debug","z":"bcbbf688.15a69","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":160,"wires":[]},{"id":"322078ba.9084b","type":"switch","z":"bcbbf688.15a69","name":"jsonata: (payload.animal = \"dog\") and (payload.color = \"black\")","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"(payload.animal = \"dog\") and (payload.color = \"black\")","vt":"jsonata"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":410,"y":220,"wires":[["6b0b4f8f.932e18"],["171a99f5.daa6be"]]},{"id":"a0f00437.ba40d","type":"debug","z":"bcbbf688.15a69","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":350,"y":60,"wires":[]},{"id":"171a99f5.daa6be","type":"debug","z":"bcbbf688.15a69","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":280,"wires":[]},{"id":"b90434be.4bd2e","type":"catch","z":"bcbbf688.15a69","name":"","scope":null,"uncaught":false,"x":250,"y":320,"wires":[["490ff05a.d5eb38"]]},{"id":"490ff05a.d5eb38","type":"debug","z":"bcbbf688.15a69","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":320,"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:** [13 March 2021 10:22 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/8 "2021-03-13T10:22:57Z")

</div>

> [@borpin](#):
>
> I have an array of objects.
> 
> I want to test a specific property of each object. If one test fails, the whole array fails and I want a single 'failed' output.
> 
> I can do this in a Javascript function but cannot work out how to do it using nodes.

Hi @borpin there are times when a function node is the just the ~~right~~ best way to go with certain things. Consider you can easily solve this with one function node vs multiple standard nodes & a whole bunch of tricks. I would personally recommend sticking with a function node for this.

---

<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:** [13 March 2021 10:28 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/9 "2021-03-13T10:28:24Z")

</div>

I would add you may need to check type as the expression will throw an error when you try to compare stings and numbers.

```auto
$type(payload.param)="number" ?	(payload.param < 10 and payload.id < 6) : false

```

without type() if param is string jsonata will give error.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 March 2021 13:12 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/10 "2021-03-13T13:12:40Z")

</div>

Indeed. The function node IS a standard node and is there to be used just as much as any other.

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [13 March 2021 15:01 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/11 "2021-03-13T15:01:27Z")

</div>

Thanks everyone, I just wondered if there was an easier way than a function.

I ended up with a function 🙂

```auto
msg.payload = msg.data.filter(function(data){
    return (data.attributes.device_class == "temperature") && ((data.attributes.last_median_of < 5) || (Date.parse(data.last_updated) + 300000) < Date.now());
}).length > 0;

```

---

<div class="post-metadata">

**Author:** ![mrdvt92](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrdvt92/32/33122_2.png) [@mrdvt92](https://discourse.nodered.org/u/mrdvt92)\
**Post date:** [18 March 2021 03:04 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/12 "2021-03-18T03:04:50Z")

</div>

I would have done it like this

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

```auto
[{"id":"bca1a0bc.8bf72","type":"inject","z":"1d254cc8.5925b3","name":"black dog","topic":"","payload":"{\"animal\":\"dog\",\"color\":\"black\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":380,"wires":[["f2fe6b6.ad16f98"]]},{"id":"f2fe6b6.ad16f98","type":"switch","z":"1d254cc8.5925b3","name":"animal=dog","property":"payload.animal","propertyType":"msg","rules":[{"t":"eq","v":"dog","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":380,"wires":[["b2dd4ee8.3dda"],["bef458a1.54af48"]]},{"id":"b2dd4ee8.3dda","type":"switch","z":"1d254cc8.5925b3","name":"color=black","property":"payload.color","propertyType":"msg","rules":[{"t":"eq","v":"black","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":490,"y":380,"wires":[["c6c68a0c.9ed648"],["bef458a1.54af48"]]},{"id":"c6c68a0c.9ed648","type":"debug","z":"1d254cc8.5925b3","name":"black dogs","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":380,"wires":[]},{"id":"bef458a1.54af48","type":"debug","z":"1d254cc8.5925b3","name":"otherwise","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":660,"y":420,"wires":[]},{"id":"a3b77595.904bf8","type":"inject","z":"1d254cc8.5925b3","name":"gray cat","topic":"","payload":"{\"animal\":\"cat\",\"color\":\"gray\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":420,"wires":[["f2fe6b6.ad16f98"]]}]

```

---

<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:** [2 April 2021 08:29 UTC](https://discourse.nodered.org/t/test-a-property-of-an-array-of-objects/42565/13 "2021-04-02T08:29:15Z")

</div>

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