# Seperating messages from array

**URL:** <https://discourse.nodered.org/t/seperating-messages-from-array/61490>\
**Category:** General\
**Tags:** function-node\
**Created:** [19 April 2022 11:26 UTC](https://discourse.nodered.org/t/seperating-messages-from-array/61490 "2022-04-19T11:26:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![developedbymayur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/developedbymayur/32/56701_2.png) [@developedbymayur](https://discourse.nodered.org/u/developedbymayur)\
**Post date:** [19 April 2022 11:26 UTC](https://discourse.nodered.org/t/seperating-messages-from-array/61490/1 "2022-04-19T11:26:57Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0f7bf7a1f2228af8e9ce8cf9e7330356900e196d.png)

hello all , im having input of array , in which displayname is different from one function node i want to seperates the message or elements into two outputs.

i want complete object to be seperated.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/14bfea2ec2322501245174f125daf6dd0319c3a6.png)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [19 April 2022 11:56 UTC](https://discourse.nodered.org/t/seperating-messages-from-array/61490/2 "2022-04-19T11:56:12Z")

</div>

Hello ..

you can loop throught the array and if the `DisplayName` startsWith either MM2 or MM3 push the array element to newly defined arrays and then output them to the seperate outputs.

```auto
let mm2 = []
let mm3 = []

msg.payload.forEach(el => {

    if (el.DisplayName.startsWith("MM2.")) {
        mm2.push(el)
    }

    else if (el.DisplayName.startsWith("MM3.")) {
        mm3.push(el)
    }
})

return [{ payload: mm2 }, { payload: mm3 }]

```

**Test Flow :**

```auto
[{"id":"4d3938590c6cf87d","type":"inject","z":"54efb553244c241f","name":"data","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"nodeID\":1,\"applicationUri\":\"aaaa\",\"DisplayName\":\"MM2.D0058\",\"Value\":\"aaaaaa\"},{\"nodeID\":2,\"applicationUri\":\"bbb\",\"DisplayName\":\"MM2.D0058\",\"Value\":\"aaaaaa\"},{\"nodeID\":3,\"applicationUri\":\"ccc\",\"DisplayName\":\"MM3.D0058\",\"Value\":\"aaaaaa\"},{\"nodeID\":4,\"applicationUri\":\"ddd\",\"DisplayName\":\"MM2.D0058\",\"Value\":\"aaaaaa\"},{\"nodeID\":5,\"applicationUri\":\"eee\",\"DisplayName\":\"MM3.D0058\",\"Value\":\"aaaaaa\"},{\"nodeID\":6,\"applicationUri\":\"fff\",\"DisplayName\":\"MM2.D0058\",\"Value\":\"aaaaaa\"}]","payloadType":"json","x":330,"y":1860,"wires":[["7a5436554cf3129e","496dfc1050b6b95e"]]},{"id":"e6ef0f9ea7d918ec","type":"debug","z":"54efb553244c241f","name":"mm2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":1800,"wires":[]},{"id":"7a5436554cf3129e","type":"function","z":"54efb553244c241f","name":"","func":"let mm2 = []\nlet mm3 = []\n\nmsg.payload.forEach(el => {\n\n if (el.DisplayName.startsWith(\"MM2.\")) {\n mm2.push(el)\n }\n\n else if (el.DisplayName.startsWith(\"MM3.\")) {\n mm3.push(el)\n }\n\n})\n\nreturn [{ payload: mm2 }, { payload: mm3 }]","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":1860,"wires":[["e6ef0f9ea7d918ec"],["7b1be89f672a7ce4"]]},{"id":"7b1be89f672a7ce4","type":"debug","z":"54efb553244c241f","name":"mm3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":1920,"wires":[]},{"id":"496dfc1050b6b95e","type":"debug","z":"54efb553244c241f","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":440,"y":1760,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [19 April 2022 12:00 UTC](https://discourse.nodered.org/t/seperating-messages-from-array/61490/3 "2022-04-19T12:00:11Z")

</div>

Or a Split node to split the array into discrete messages, and a Switch node to route each message depending on the message property.

---

<div class="post-metadata">

**Author:** ![developedbymayur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/developedbymayur/32/56701_2.png) [@developedbymayur](https://discourse.nodered.org/u/developedbymayur)\
**Post date:** [19 April 2022 12:47 UTC](https://discourse.nodered.org/t/seperating-messages-from-array/61490/4 "2022-04-19T12:47:55Z")

</div>

Thank you for your help , just wanted a small correction to handle when in a packet sometimes there may not be mm2 or sometimes may not be mm3

---

<div class="post-metadata">

**Author:** ![developedbymayur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/developedbymayur/32/56701_2.png) [@developedbymayur](https://discourse.nodered.org/u/developedbymayur)\
**Post date:** [19 April 2022 12:57 UTC](https://discourse.nodered.org/t/seperating-messages-from-array/61490/5 "2022-04-19T12:57:13Z")

</div>

(post deleted by author)

---

<div class="post-metadata">

**Author:** ![developedbymayur](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/developedbymayur/32/56701_2.png) [@developedbymayur](https://discourse.nodered.org/u/developedbymayur)\
**Post date:** [19 April 2022 12:59 UTC](https://discourse.nodered.org/t/seperating-messages-from-array/61490/6 "2022-04-19T12:59:11Z")

</div>

````auto
  {
    "NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0055",
    "ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
    "DisplayName": "MM2.D0055",
    "Value": {
      "Value": 256.33554,
      "SourceTimestamp": "2022-04-19T12:53:19.551Z"
    }
  },
  {
    "NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0056",
    "ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
    "DisplayName": "MM2.D0056",
    "Value": {
      "Value": 52.6,
      "SourceTimestamp": "2022-04-19T12:52:40.558Z"
    }
  },
  {
    "NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0058",
    "ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
    "DisplayName": "MM2.D0058",
    "Value": {
      "Value": -0.061722744,
      "SourceTimestamp": "2022-04-19T12:53:19.551Z"
    }
  },
  {
    "NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0289",
    "ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
    "DisplayName": "MM2.D0289",
    "Value": {
      "Value": 256.33554,
      "SourceTimestamp": "2022-04-19T12:53:19.549Z"
    }
  },
  {
    "NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0290",
    "ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
    "DisplayName": "MM2.D0290",
    "Value": {
      "Value": 79.8,
      "SourceTimestamp": "2022-04-19T12:53:01.561Z"
    }
  },
  {
    "NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0302",
    "ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
    "DisplayName": "MM2.D0302",
    "Value": {
      "Value": 256.72006,
      "SourceTimestamp": "2022-04-19T12:53:16.565Z"
    }
  },
  {
    "NodeId": "nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0133",
    "ApplicationUri": "urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server",
    "DisplayName": "MM2.D0133",
    "Value": {
      "Value": true,
      "SourceTimestamp": "2022-04-19T12:53:46.552Z"
    }
  }
]```
````

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [19 April 2022 13:19 UTC](https://discourse.nodered.org/t/seperating-messages-from-array/61490/7 "2022-04-19T13:19:47Z")

</div>

Test flow to handle case when some values are missing ( array length is 0 )

```auto
[{"id":"4d3938590c6cf87d","type":"inject","z":"54efb553244c241f","name":"data without mm3","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0055\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0055\",\"Value\":{\"Value\":256.33554,\"SourceTimestamp\":\"2022-04-19T12:53:19.551Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0056\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0056\",\"Value\":{\"Value\":52.6,\"SourceTimestamp\":\"2022-04-19T12:52:40.558Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0058\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0058\",\"Value\":{\"Value\":-0.061722744,\"SourceTimestamp\":\"2022-04-19T12:53:19.551Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0289\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0289\",\"Value\":{\"Value\":256.33554,\"SourceTimestamp\":\"2022-04-19T12:53:19.549Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0290\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0290\",\"Value\":{\"Value\":79.8,\"SourceTimestamp\":\"2022-04-19T12:53:01.561Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0302\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0302\",\"Value\":{\"Value\":256.72006,\"SourceTimestamp\":\"2022-04-19T12:53:16.565Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0133\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0133\",\"Value\":{\"Value\":true,\"SourceTimestamp\":\"2022-04-19T12:53:46.552Z\"}}]","payloadType":"json","x":260,"y":1840,"wires":[["7a5436554cf3129e"]]},{"id":"e6ef0f9ea7d918ec","type":"debug","z":"54efb553244c241f","name":"mm2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":1840,"wires":[]},{"id":"7a5436554cf3129e","type":"function","z":"54efb553244c241f","name":"","func":"let mm2 = { topic: \"mm2\", payload: [] }\nlet mm3 = { topic: \"mm3\", payload: [] }\n\nmsg.payload.forEach(el => {\n\n if (el.DisplayName.startsWith(\"MM2.\")) {\n mm2.payload.push(el)\n }\n\n else if (el.DisplayName.startsWith(\"MM3.\")) {\n mm3.payload.push(el)\n }\n\n})\n\nif (mm2.payload.length == 0) mm2 = null\nif (mm3.payload.length == 0) mm3 = null\n\nreturn [mm2, mm3]","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":1880,"wires":[["e6ef0f9ea7d918ec"],["7b1be89f672a7ce4"]]},{"id":"7b1be89f672a7ce4","type":"debug","z":"54efb553244c241f","name":"mm3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":1920,"wires":[]},{"id":"deeef12456b127cf","type":"inject","z":"54efb553244c241f","name":"data with both","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0055\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0055\",\"Value\":{\"Value\":256.33554,\"SourceTimestamp\":\"2022-04-19T12:53:19.551Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0056\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0056\",\"Value\":{\"Value\":52.6,\"SourceTimestamp\":\"2022-04-19T12:52:40.558Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0058\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM3.D0058\",\"Value\":{\"Value\":-0.061722744,\"SourceTimestamp\":\"2022-04-19T12:53:19.551Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0289\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0289\",\"Value\":{\"Value\":256.33554,\"SourceTimestamp\":\"2022-04-19T12:53:19.549Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0290\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM3.D0290\",\"Value\":{\"Value\":79.8,\"SourceTimestamp\":\"2022-04-19T12:53:01.561Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0302\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0302\",\"Value\":{\"Value\":256.72006,\"SourceTimestamp\":\"2022-04-19T12:53:16.565Z\"}},{\"NodeId\":\"nsu=KEPServerEX;s=INPI_P1_H1_MM2.PH1_MM2.MM.D0133\",\"ApplicationUri\":\"urn:ABCDEFGHIJKL.XYZ.COMM:Kepware.KEPServerEX.V6:UA%20Server\",\"DisplayName\":\"MM2.D0133\",\"Value\":{\"Value\":true,\"SourceTimestamp\":\"2022-04-19T12:53:46.552Z\"}}]","payloadType":"json","x":270,"y":1920,"wires":[["7a5436554cf3129e"]]}]

```

---

<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:** [18 June 2022 13:20 UTC](https://discourse.nodered.org/t/seperating-messages-from-array/61490/8 "2022-06-18T13:20:03Z")

</div>

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