Skip to content

Instantly share code, notes, and snippets.

@ozomer

ozomer/README.md Secret

Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozomer/139d4107a2483732904f to your computer and use it in GitHub Desktop.
Save ozomer/139d4107a2483732904f to your computer and use it in GitHub Desktop.
Array Map Example

This flow exemplifies how to split an array to separate messages, manipulate them individually, and the reconstruct them to an array.

The example uses the "external reference" trick similar to: http://flows.nodered.org/flow/810bc88d27f705510093

Warning: This trick is based on the fact that when the "clone" package clones functions, it does not clone their scope. The behavior may change in the future.

[{"id":"bea747f1.4158b8","type":"inject","name":"Array of low-letter messages","topic":"","payload":"[\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\"]","payloadType":"string","repeat":"","crontab":"","once":false,"x":173,"y":83.72224521636963,"z":"a2b633a9.5d49d","wires":[["f42cd6b.f0bd328"]]},{"id":"c2852efd.3d7ad","type":"debug","name":"","active":true,"console":"false","complete":"payload","x":600.888916015625,"y":206.27780151367188,"z":"a2b633a9.5d49d","wires":[]},{"id":"f42cd6b.f0bd328","type":"json","name":"","x":390.8333053588867,"y":83,"z":"a2b633a9.5d49d","wires":[["663e1cfb.99c1e4"]]},{"id":"a541dc43.5abe2","type":"function","name":"manipulate each element","func":"msg.payload = msg.payload.toUpperCase();\nreturn msg;","outputs":1,"x":198.6111068725586,"y":206.333345413208,"z":"a2b633a9.5d49d","wires":[["cc207b5b.33df88"]]},{"id":"cc207b5b.33df88","type":"function","name":"reconstruct array","func":"var mapResult = msg.mapResult();\nmapResult.result[msg.index] = msg.payload;\nmapResult.countdown -= 1;\nif (0 === mapResult.countdown) {\n return {\n payload: mapResult.result\n };\n}\n","outputs":1,"x":424.1666488647461,"y":206.33334350585938,"z":"a2b633a9.5d49d","wires":[["c2852efd.3d7ad"]]},{"id":"663e1cfb.99c1e4","type":"function","name":"split elements","func":"var mapResult = {\n countdown: msg.payload.length,\n result: msg.payload.map(function(){})\n};\nreturn [msg.payload.map(function(element, index) {\n return {\n payload: element,\n index: index,\n mapResult: function() {\n return mapResult;\n }\n };\n})];\n","outputs":1,"x":574,"y":83,"z":"a2b633a9.5d49d","wires":[["a541dc43.5abe2"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment