# Convert all values in array

**URL:** <https://discourse.nodered.org/t/convert-all-values-in-array/25853>\
**Category:** General\
**Created:** [1 May 2020 09:19 UTC](https://discourse.nodered.org/t/convert-all-values-in-array/25853 "2020-05-01T09:19:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrewZ](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@AndrewZ](https://discourse.nodered.org/u/AndrewZ)\
**Post date:** [1 May 2020 09:19 UTC](https://discourse.nodered.org/t/convert-all-values-in-array/25853/1 "2020-05-01T09:19:32Z")

</div>

This is one of my first exercises with Node-RED, so please be kind 😉  
I'm creating a simple dashboard to control my Modbus devices. Currently I can see the status of a few inputs and control the state of a few outputs. Now I want to slightly optimize the design.  
_Modbus-Read_ node gives me the following input:

```auto
msg.payload : array[8]
[false, false, false, false, false, false, false, false]

```

How can I convert [at once] each and every value in the array from _true_ to _On_ and _false_ to _Off_?  
Currently I'm converting every value like `{{msg.payload[5]}}` with individual _Change_ nodes which is not nice.

---

<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:** [1 May 2020 09:22 UTC](https://discourse.nodered.org/t/convert-all-values-in-array/25853/2 "2020-05-01T09:22:29Z")

</div>

one option - use a function node and [array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

give it a go.

If you get stuck, come back.

EDIT...  
Heres a big sledgehammer clue...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/6/963208b211ec9b1ec0c989c7ac714c53e98dddc6.png)

---

<div class="post-metadata">

**Author:** ![AndrewZ](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@AndrewZ](https://discourse.nodered.org/u/AndrewZ)\
**Post date:** [1 May 2020 09:48 UTC](https://discourse.nodered.org/t/convert-all-values-in-array/25853/3 "2020-05-01T09:48:42Z")

</div>

Many thanks! Working with the following code:

```auto
var data = msg.payload;
msg.payload = data.map(x => x ? "On" : "Off");
return msg;

```

---

<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:** [15 May 2020 09:48 UTC](https://discourse.nodered.org/t/convert-all-values-in-array/25853/4 "2020-05-15T09:48:46Z")

</div>

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