Hi @Colin @Aviation is telling the function that localStation should = flow.dataStation and make sure its an array, then he/she goes on to fill localStation with some data which happens to be an object and pushes that to localStation array. Aviation then uses flow.set (At this point, I'm making a assumption/guess that flow.set is actually sugar syntax for whatever type its dealing with. E.g. array would be another array.push, objects would overwrite themselves) to add the data to the flow context. Effectively it's pushing an array to an array. We want to mutate the existing array!
I used the following reference from StackOverflow which explains it better than I could: https://stackoverflow.com/a/42428064/2235420
This achieves the data structure you are after:
var localStations = flow.get("dataStations");
// localStations = ( typeof localStations != "undefined" && localStations instanceof Array ) ? localStations : [];
function appendObjTo(thatArray, newObj) {
const frozenObj = Object.freeze(newObj);
return Object.freeze(thatArray.concat(frozenObj));
}
function station(station, diesel, e5, e10) {
return {
station: station,
diesel: diesel,
e5: e5,
e10: e10
};
}
// localStations.push({ key: 0, station: station(1, 2, 3, 4) });
const appendedLocalStations = appendObjTo(localStations, { key: 0, station: station(1, 2, 3, 4) })
flow.set("dataStations", appendedLocalStations);
return appendedLocalStations;
The flow I used as reference also:
[{"id":"ede3744e.eed578","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"e074d7e.4c4fd28","type":"inject","z":"ede3744e.eed578","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":100,"wires":[["40f58adf.654244"]]},{"id":"40f58adf.654244","type":"function","z":"ede3744e.eed578","name":"push to dataStations","func":"var localStations = flow.get(\"dataStations\");\n// localStations = ( typeof localStations != \"undefined\" && localStations instanceof Array ) ? localStations : [];\n\nfunction appendObjTo(thatArray, newObj) {\n const frozenObj = Object.freeze(newObj);\n return Object.freeze(thatArray.concat(frozenObj));\n}\n\nfunction station(station, diesel, e5, e10) {\n return {\n station: station,\n diesel: diesel,\n e5: e5,\n e10: e10\n };\n}\n\n// localStations.push({ key: 0, station: station(1, 2, 3, 4) });\n\nconst appendedLocalStations = appendObjTo(localStations, { key: 0, station: station(1, 2, 3, 4) })\n\nflow.set(\"dataStations\", appendedLocalStations);\n\nreturn appendedLocalStations;","outputs":1,"noerr":0,"x":375,"y":100,"wires":[[]]},{"id":"9ec2b03e.3d83d","type":"inject","z":"ede3744e.eed578","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":110,"y":50,"wires":[["a46427b4.825978"]]},{"id":"a46427b4.825978","type":"function","z":"ede3744e.eed578","name":"emptyArray","func":"flow.set(\"dataStations\", new Array());\nreturn msg;","outputs":1,"noerr":0,"x":345,"y":50,"wires":[[]]}]