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

**URL:** <https://discourse.nodered.org/t/change-node-to-set-value-for-each-object-in-array-with-if-exeption/70671>\
**Category:** General\
**Created:** [15 November 2022 20:57 UTC](https://discourse.nodered.org/t/change-node-to-set-value-for-each-object-in-array-with-if-exeption/70671 "2022-11-15T20:57:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danieleronchi](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@danieleronchi](https://discourse.nodered.org/u/danieleronchi)\
**Post date:** [15 November 2022 20:57 UTC](https://discourse.nodered.org/t/change-node-to-set-value-for-each-object-in-array-with-if-exeption/70671/1 "2022-11-15T20:57:05Z")

</div>

Hi to everyone,  
i need a little help. I found this intresting discussion [Change node to set value for each object in array](https://discourse.nodered.org/t/change-node-to-set-value-for-each-object-in-array/55853) 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.

---

<div class="post-metadata">

**Author:** ![knoepsche](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knoepsche/32/69450_2.png) [@knoepsche](https://discourse.nodered.org/u/knoepsche)\
**Post date:** [16 November 2022 09:33 UTC](https://discourse.nodered.org/t/change-node-to-set-value-for-each-object-in-array-with-if-exeption/70671/2 "2022-11-16T09:33:09Z")

</div>

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

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

```

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

---

<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:** [16 November 2022 10:27 UTC](https://discourse.nodered.org/t/change-node-to-set-value-for-each-object-in-array-with-if-exeption/70671/3 "2022-11-16T10:27:32Z")

</div>

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

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

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

```

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

---

<div class="post-metadata">

**Author:** ![danieleronchi](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@danieleronchi](https://discourse.nodered.org/u/danieleronchi)\
**Post date:** [17 November 2022 19:44 UTC](https://discourse.nodered.org/t/change-node-to-set-value-for-each-object-in-array-with-if-exeption/70671/4 "2022-11-17T19:44:06Z")

</div>

Omg, thank you so much. It work perfectly!

---

<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:** [1 December 2022 19:44 UTC](https://discourse.nodered.org/t/change-node-to-set-value-for-each-object-in-array-with-if-exeption/70671/5 "2022-12-01T19:44:48Z")

</div>

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