# Splitting an array of objects

**URL:** <https://discourse.nodered.org/t/splitting-an-array-of-objects/37815>\
**Category:** General\
**Created:** [20 December 2020 20:54 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815 "2020-12-20T20:54:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [20 December 2020 20:54 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/1 "2020-12-20T20:54:01Z")

</div>

Back again with what should be a simple solution, but over 5 hours in...I am still unable to accomplish, even with going through the basic videos and a multitude of other posts in the forum. I have an array of objects that looks like this from a SQL Select statement:

![array](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b721064e0de7180cf62137c2dedfac56f4d2a142.png)

I simply wish to output each of the unique objects to a text UI for display. Tried many variations of the split node and also the switch node (with multiple outputs) but no go. Each time with debug at the end to evaluate the message, but always blank. Thanks in advance for guidance.

---

<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:** [20 December 2020 20:59 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/2 "2020-12-20T20:59:14Z")

</div>

> [@Tomadoggy](#):
>
> I simply wish to output each of the unique objects to a text UI for display

How would you expect to display 2 or more rows each with multiple properties in "a text ui"?

Please provide details and scenarios.

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [20 December 2020 21:04 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/3 "2020-12-20T21:04:06Z")

</div>

My goal is to split out each of the listed objects above then send the associated value over to the UI text node (appropriately labeled of course). If I can just separate the objects from the array...I think I can take it from there.

---

<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:** [20 December 2020 21:12 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/4 "2020-12-20T21:12:22Z")

</div>

But what do you expect to turn these array of objects into?

There are an unknown amount of array entries & each object in the array has 4 properties.

So if there is only 1 row, what would you expect to come out of this "function"?

- A single object with the 4 named properties?  
Or
- 4 messages with the payload set to the value and the topic set to the property name?  
Or
- 1 message with all 4 properties as a comma separated string?  
Or
- Something else?

Then what if there are 2 rows or more rows?

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [20 December 2020 21:15 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/5 "2020-12-20T21:15:12Z")

</div>

> [@Steve-Mcl](#):
>
> 4 messages with the payload set to the value and the topic set to the property name?  
> Or

I believe this would be the option that I could work with in the most straight forward fashion.

---

<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:** [20 December 2020 21:22 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/6 "2020-12-20T21:22:17Z")

</div>

I think there is some confusion on terminology.

you wish to display the object property values in a text ui?

i.e.

```auto
[{"id":"93030c34.f30f18","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"highest\":45678,\"min(altitude\":34563,\"min(temp)\":20,\"max(temp)\":37}]","payloadType":"json","x":160,"y":2300,"wires":[["5b496a9b.82a93c"]]},{"id":"5b496a9b.82a93c","type":"ui_text","z":"8d22ae29.7df6d","group":"165e1e1a.e752fa","order":6,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload[0][\"min(altitude\"]}}","layout":"row-spread","x":340,"y":2320,"wires":[]},{"id":"165e1e1a.e752fa","type":"ui_group","z":"","name":"Default","tab":"f9b58883.fa613","order":1,"disp":true,"width":"12","collapse":false},{"id":"f9b58883.fa613","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

msg.payload[0]["min(altitude"]

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [20 December 2020 21:23 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/7 "2020-12-20T21:23:57Z")

</div>

Yes, that is the desired outcome.

---

<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:** [20 December 2020 21:32 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/8 "2020-12-20T21:32:09Z")

</div>

What do you want to happen if there is more than one element in the array?

---

<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:** [20 December 2020 21:33 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/9 "2020-12-20T21:33:59Z")

</div>

> [@Colin](#):
>
> What do you want to happen if there is more than one element in the array

That is what I was trying to get clarification on

  

@Tomadoggy this will do what you asked - fully dynamic / works for multiple rows & any amount of columns...

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

The function code

```auto
for (let i = 0; i < msg.payload.length; i++) {
    const el = msg.payload[i];
    const entries = Object.entries(el).map(([topic, payload]) => ({ topic, payload, row: (i+1) }));
    node.send([entries]);
}

```

---

<div class="post-metadata">

**Author:** ![Tomadoggy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tomadoggy/32/34065_2.png) [@Tomadoggy](https://discourse.nodered.org/u/Tomadoggy)\
**Post date:** [20 December 2020 21:47 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/10 "2020-12-20T21:47:07Z")

</div>

@Steve-Mcl So many thanks, your function followed by switch node discrete by msg.topic does the trick quite nicely. Could have not done this without you 💯

Flow below in the event that anyone else may be able to benefit:

```auto
[{"id":"a5e06468.8b1ec8","type":"ui_form","z":"70fc13ea.5d03dc","name":"Input Date Range for History","label":" ","group":"b5ec3e8f.47604","order":15,"width":0,"height":0,"options":[{"label":"Start Date","value":"startDate","type":"date","required":true,"rows":null},{"label":"End Date","value":"endDate","type":"date","required":true,"rows":null}],"formValue":{"startDate":"","endDate":""},"payload":"","submit":"submit","cancel":"cancel","topic":"","x":390,"y":110,"wires":[["82f9af3b.17ab2"]]},{"id":"82f9af3b.17ab2","type":"function","z":"70fc13ea.5d03dc","name":"Select min/max","func":"let startDateTime = msg.payload.startDate.slice(0, 10) + \" \" + msg.payload.startTime\nlet endDateTime = msg.payload.endDate.slice(0, 10) + \" \" + msg.payload.endTime\n\n//msg.topic = `SELECT MAX(Altitude), MIN(Altitude) , MAX(Temp), MIN(Temp) FROM exterior_climate WHERE Timestamp BETWEEN '${startDateTime}' AND '${endDateTime}'`\nmsg.topic = `SELECT MAX(Altitude) AS MaxA, MIN(Altitude) AS MinA, MAX(Temp) AS MaxT, MIN(Temp) AS MinT FROM exterior_climate WHERE Timestamp BETWEEN '${startDateTime}' AND '${endDateTime}'`\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":385,"y":280,"wires":[["8fc8ff52.12865"]]},{"id":"8fc8ff52.12865","type":"mysql","z":"70fc13ea.5d03dc","mydb":"9b6527f7.cad8b8","name":"MYSQL","x":585,"y":280,"wires":[["7f12c8e4.c82998"]]},{"id":"7f12c8e4.c82998","type":"function","z":"70fc13ea.5d03dc","name":"Split into Topics","func":"for (let i = 0; i < msg.payload.length; i++) {\n const el = msg.payload[i];\n const entries = Object.entries(el).map(([key, value]) => ({ topic: key, payload: value }));\n node.send([entries]);\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":795,"y":280,"wires":[["b0671e81.ca3b6"]]},{"id":"b0671e81.ca3b6","type":"switch","z":"70fc13ea.5d03dc","name":"Split outputs","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"MaxA","vt":"str"},{"t":"eq","v":"MinA","vt":"str"},{"t":"eq","v":"MaxT","vt":"str"},{"t":"eq","v":"MinT","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":985,"y":290,"wires":[["182561af.897b4e"],["a3a0c418.223d68"],["1c62cb50.b20365"],["9297ba5b.193e08"]],"outputLabels":["MAX Altitude","MIN Altitude","MAx Temp","MIN Temp"]},{"id":"182561af.897b4e","type":"ui_text","z":"70fc13ea.5d03dc","group":"b5ec3e8f.47604","order":20,"width":6,"height":1,"name":"Max Altitude","label":"Max Altitude: ","format":"{{msg.payload}}","layout":"row-left","x":1305,"y":270,"wires":[]},{"id":"a3a0c418.223d68","type":"ui_text","z":"70fc13ea.5d03dc","group":"b5ec3e8f.47604","order":22,"width":6,"height":1,"name":"Min Altitude","label":"Min Altitude: ","format":"{{msg.payload}}","layout":"row-left","x":1305,"y":305,"wires":[]},{"id":"1c62cb50.b20365","type":"ui_text","z":"70fc13ea.5d03dc","group":"b5ec3e8f.47604","order":21,"width":6,"height":1,"name":"Max Temp","label":"Max Temp: ","format":"{{msg.payload}}","layout":"row-left","x":1305,"y":341,"wires":[]},{"id":"9297ba5b.193e08","type":"ui_text","z":"70fc13ea.5d03dc","group":"b5ec3e8f.47604","order":23,"width":6,"height":1,"name":"Min Temp","label":"Min Temp: ","format":"{{msg.payload}}","layout":"row-left","x":1295,"y":377,"wires":[]},{"id":"b5ec3e8f.47604","type":"ui_group","name":"Climate - Exterior","tab":"c2eeb8ac.68fa38","order":1,"disp":true,"width":"24","collapse":false},{"id":"9b6527f7.cad8b8","type":"MySQLdatabase","name":"","host":"localhost","port":"3306","db":"mqtt_data","tz":"","charset":"UTF8"},{"id":"c2eeb8ac.68fa38","type":"ui_tab","name":"Climate - Exterior","icon":"wb_sunny","order":2,"disabled":false,"hidden":false,"info":"help"}]

```

---

<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:** [3 January 2021 21:47 UTC](https://discourse.nodered.org/t/splitting-an-array-of-objects/37815/11 "2021-01-03T21:47:09Z")

</div>

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