Delete Object pair based on its value

Hi all, trying to remove noise from being input into my database. Any value below 0 to be noise due to electrical resistance etc.

This is an object similar to the output of the Analog Input Module

{
   "AL1":5,
   "AL2":6,
   "AL3":-2
}

Currently the function im using

if(msg.payload.AL1<0)delete msg.payload.AL1
if(msg.payload.AL2<0)delete msg.payload.AL2
if(msg.payload.AL3<0)delete msg.payload.AL3

Desired Outcome

{
   "AL1":5,
   "AL2":6
}

The issue is that there is a total of 12 AI modules with 6 variables each and I cant keep hardcoding the condition check.
Im sure there is a better way to do this but im quite lost on how to do it. JS Objects are unorded so I cant run a for loop and delete it based on its index. Well unless im wrong

Try something like this

for(const key in msg.payload){
    if(msg.payload[key] < 0 ){
        delete msg.payload[key];
    }
}
return msg;
2 Likes

@E1cid
Many thanks. You always solve my Json problems

1 Like

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