# Array split join basics

**URL:** <https://discourse.nodered.org/t/array-split-join-basics/33780>\
**Category:** General\
**Created:** [5 October 2020 16:48 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780 "2020-10-05T16:48:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjmotew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sjmotew/32/29709_2.png) [@sjmotew](https://discourse.nodered.org/u/sjmotew)\
**Post date:** [5 October 2020 16:48 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/1 "2020-10-05T16:48:29Z")

</div>

Hi. There are many posts regarding split:join arrays through function, however, I am having problems finding what I think should be relatively simple:

1. Extract a single part from each array object, in this case "msg.payload.state"
2. Remove all other msg.payload attributes
3. Reassemble the array objects with only the msg.payload[i] = msg.payload,state[i]

Note, the array is entering NR pre-assembled with each object hosting multiple msg.payload components.

Thanks.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [5 October 2020 17:34 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/2 "2020-10-05T17:34:32Z")

</div>

Hi,

anything is possible with javascript in a function.  
Is it possible to copy/paste a sample of your data to work with ?  
5-6 elements of the array of objects will do .. just to have some real data to experiment with

---

<div class="post-metadata">

**Author:** ![sjmotew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sjmotew/32/29709_2.png) [@sjmotew](https://discourse.nodered.org/u/sjmotew)\
**Post date:** [5 October 2020 18:06 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/3 "2020-10-05T18:06:30Z")

</div>

Thanks, @UnborN. I think I figured it out. Added split node from Array then switch node to change msg.payload = msg.payload.state and join. Viola.

```auto
[{"id":"b4101700.63b118","type":"debug","z":"bf354502.549c58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1130,"y":1140,"wires":[]},{"id":"538d44ce.3173ec","type":"join","z":"bf354502.549c58","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":910,"y":1140,"wires":[["b4101700.63b118"]]},{"id":"e243b109.cf2a","type":"split","z":"bf354502.549c58","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":570,"y":1140,"wires":[["ef7bfa9.ce70b08"]]},{"id":"ef7bfa9.ce70b08","type":"change","z":"bf354502.549c58","name":"Change State","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.state","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":1140,"wires":[["538d44ce.3173ec"]]}]

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [5 October 2020 18:13 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/4 "2020-10-05T18:13:12Z")

</div>

It seems that you have taken the long path 😉  
Out of curiosity have you tried using **only** a Change node with **Move** setting ?  
Does this produce the desired result ?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9a0b75517737c12a403b5a5b66f8fbd7e80816cf.png)

---

<div class="post-metadata">

**Author:** ![sjmotew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sjmotew/32/29709_2.png) [@sjmotew](https://discourse.nodered.org/u/sjmotew)\
**Post date:** [5 October 2020 18:36 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/5 "2020-10-05T18:36:20Z")

</div>

The use of change node would be great, will it work for all of the objects in the array or do I need to split it first?

S

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [5 October 2020 18:41 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/6 "2020-10-05T18:41:22Z")

</div>

You dont need to split the array and then join in afterwards  
The Move option in Change node should move anything under msg.payload.state to msg.payload  
whether thats an array, object, string, number etc

Only one way to find out 😉  
do another wire from your data to the Change node with the Move setting and an extra debug node and test it 😉

---

<div class="post-metadata">

**Author:** ![sjmotew](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sjmotew/32/29709_2.png) [@sjmotew](https://discourse.nodered.org/u/sjmotew)\
**Post date:** [5 October 2020 18:50 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/7 "2020-10-05T18:50:06Z")

</div>

When putting change/move, then the msg.payload returns "undefined", split then move then join works fine.

Thanks!

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [5 October 2020 19:04 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/8 "2020-10-05T19:04:51Z")

</div>

you are right .. i did a quick test with fake data and it is as you say

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 October 2020 20:00 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/9 "2020-10-05T20:00:57Z")

</div>

If you want to change each array object, you would need to use JSONata in a change node for that. But I think that might just add to the confusion here (another thing to learn).

In a function node, you will want to make use of the array function `.forEach()` - assuming your array is in msg.payload:

```auto
msg.payload.forEach( obj => {
    // now `obj` contains each object in the array in turn.
})
```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 October 2020 20:33 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/10 "2020-10-05T20:33:56Z")

</div>

A function node containing this should do it

```auto
msg.payload = msg.payload.map(x => x.state)
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:** [4 December 2020 20:33 UTC](https://discourse.nodered.org/t/array-split-join-basics/33780/11 "2020-12-04T20:33:57Z")

</div>

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