# Help with filtering an empty object in a function

**URL:** <https://discourse.nodered.org/t/help-with-filtering-an-empty-object-in-a-function/75194>\
**Category:** General\
**Created:** [14 February 2023 18:35 UTC](https://discourse.nodered.org/t/help-with-filtering-an-empty-object-in-a-function/75194 "2023-02-14T18:35:29Z")\
**Posts on this page:** 6\
**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:** [14 February 2023 18:35 UTC](https://discourse.nodered.org/t/help-with-filtering-an-empty-object-in-a-function/75194/1 "2023-02-14T18:35:29Z")

</div>

I can't seem to get my if statement to work with an empty object.

```auto
for (let param of msg.payload.channels) {
    if (param !== {}) {
        node.send({
            parameter: param,
        });  
     }
}

```

Using a switch node work as expected with msg.parameter "is not empty".

```auto
[{"id":"e90aece12d7b56fc","type":"function","z":"d03c03b6d2f91f47","name":"All Parts","func":"for (let param of msg.payload.channels) {\n if (param !== {}) {\n node.send({\n parameter: param,\n }); \n }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1200,"y":260,"wires":[["5b8842ec80adfc4e","c53f42c899c0f961"]]},{"id":"576ee696c5fef7df","type":"debug","z":"d03c03b6d2f91f47","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1540,"y":260,"wires":[]},{"id":"5b8842ec80adfc4e","type":"switch","z":"d03c03b6d2f91f47","name":"","property":"parameter","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":1390,"y":260,"wires":[["576ee696c5fef7df"]]},{"id":"c53f42c899c0f961","type":"debug","z":"d03c03b6d2f91f47","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1540,"y":320,"wires":[]},{"id":"c5f9ab967884f3ac","type":"inject","z":"d03c03b6d2f91f47","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"serialNum\":\"EjRWeA==\",\"batteryStatus\":true,\"measurementPeriodBase\":180,\"channels\":[{\"type\":\"MEASUREMENT_TYPE_TEMPERATURE\",\"timestamp\":1676391480,\"startPoint\":1180,\"sampleOffsets\":[0,340]},{\"type\":\"MEASUREMENT_TYPE_HUMIDITY\",\"timestamp\":1676391480,\"startPoint\":52,\"sampleOffsets\":[0,-51]},{}],\"nextTransmissionAt\":1676402726,\"transferReason\":1,\"measurementPeriodFactor\":1}","payloadType":"str","x":830,"y":260,"wires":[["4e2e23abef70c478","6969b7f6cf144080"]]},{"id":"6969b7f6cf144080","type":"function","z":"d03c03b6d2f91f47","name":"function 15","func":"msg.payload = { \"serialNum\": \"EjRWeA==\", \"batteryStatus\": true, \"measurementPeriodBase\": 180, \"channels\": [{ \"type\": \"MEASUREMENT_TYPE_TEMPERATURE\", \"timestamp\": 1676391480, \"startPoint\": 1180, \"sampleOffsets\": [0, 340] }, { \"type\": \"MEASUREMENT_TYPE_HUMIDITY\", \"timestamp\": 1676391480, \"startPoint\": 52, \"sampleOffsets\": [0, -51] }, {}], \"nextTransmissionAt\": 1676402726, \"transferReason\": 1, \"measurementPeriodFactor\": 1 }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1010,"y":260,"wires":[["e90aece12d7b56fc"]]}]

```

There should only be two results and one of them is empty.

---

<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:** [14 February 2023 18:46 UTC](https://discourse.nodered.org/t/help-with-filtering-an-empty-object-in-a-function/75194/2 "2023-02-14T18:46:09Z")

</div>

> [@JohnnyPicnic](#):
>
> `if (param !== {})`

Try this instead

```auto
if (Object.keys(param || {}).length > 0)

```

---

<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:** [14 February 2023 19:23 UTC](https://discourse.nodered.org/t/help-with-filtering-an-empty-object-in-a-function/75194/3 "2023-02-14T19:23:58Z")

</div>

> [@Steve-Mcl](#):
>
> `if (Object.keys(param || {}).length > 0)`

Thank you. I like how you said try knowing full well that it will work 😛

---

<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:** [14 February 2023 19:25 UTC](https://discourse.nodered.org/t/help-with-filtering-an-empty-object-in-a-function/75194/4 "2023-02-14T19:25:16Z")

</div>

> [@JohnnyPicnic](#):
>
> I like how you said try knowing full well that it will work

It was untested and I wasn't 100% certain what you were really wanting 🤪

Glad it works

---

<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:** [14 February 2023 19:29 UTC](https://discourse.nodered.org/t/help-with-filtering-an-empty-object-in-a-function/75194/5 "2023-02-14T19:29:38Z")

</div>

Just for info, this shorter version should also work

```auto
if(Object.keys(param).length){

```

As 0 is considered falsy

---

<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:** [28 February 2023 19:30 UTC](https://discourse.nodered.org/t/help-with-filtering-an-empty-object-in-a-function/75194/6 "2023-02-28T19:30:22Z")

</div>

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