Change node to set value for each object in array with if exeption

Hi to everyone,
i need a little help. I found this intresting discussion Change node to set value for each object in array in the forum and it helps a lot but i need to change the value of the object of the array only when the value is "1" not for each object. It is possible with an if or something similar?

Thanks a lot for the help.

To extend the solution from Steve-Mcl in your linked discussion by an if statement do:

const seg = flow.get("seg");
seg.forEach(e => {if(e.on == 1){...change e...}});

or what do you mean with value is "1"?

To do so in a change node you can use transform syntax and a ternary statement.
e.g

[{"id":"47918bd2289f985c","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"one\":1,\"two\":2},{\"one\":2,\"two\":2}]","payloadType":"json","x":110,"y":3920,"wires":[["cdc9af5490fc52a1"]]},{"id":"cdc9af5490fc52a1","type":"change","z":"452103ea51141731","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload ~> |$|{\"one\": $.one = 1 ? 3 : $.one}|","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":3920,"wires":[["56bf43241f9cde0b"]]},{"id":"56bf43241f9cde0b","type":"debug","z":"452103ea51141731","name":"debug 109","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":3920,"wires":[]}]

Expression for the example object, you will need to edit for your array object as you failed to provide an example.

$$.payload ~> |$|{"one": ($.one = 1 ? 3 : $.one)}|

Any property named one with value 1 will be set to 3.

Omg, thank you so much. It work perfectly!

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