Splitting an array of objects

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

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.

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

Please provide details and scenarios.

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.

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?

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

I think there is some confusion on terminology.

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

i.e.

[{"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"]

Yes, that is the desired outcome.

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

The function code

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]);
}
3 Likes

@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 :100:

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

[{"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"}]
2 Likes

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