Help with filtering an empty object in a function

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

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".

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

Try this instead

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

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

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

Glad it works

Just for info, this shorter version should also work

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

As 0 is considered falsy

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