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"]]}]
ozomer

Flow Info

created 2 years, 11 months ago

Node Types

Core
  • debug (x1)
  • function (x3)
  • inject (x1)
  • json (x1)

Tags

  • reference
  • array
  • map
  • splitter
  • split
  • join
  • scatter
  • gather
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option