# Splitting an array of 7, and setting an topic for each message

**URL:** <https://discourse.nodered.org/t/splitting-an-array-of-7-and-setting-an-topic-for-each-message/54980>\
**Category:** General\
**Tags:** function-node\
**Created:** [10 December 2021 06:33 UTC](https://discourse.nodered.org/t/splitting-an-array-of-7-and-setting-an-topic-for-each-message/54980 "2021-12-10T06:33:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![monsivar](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/monsivar/32/40032_2.png) [@monsivar](https://discourse.nodered.org/u/monsivar)\
**Post date:** [10 December 2021 06:33 UTC](https://discourse.nodered.org/t/splitting-an-array-of-7-and-setting-an-topic-for-each-message/54980/1 "2021-12-10T06:33:37Z")

</div>

Hi

I got an function for splitting an array that contains last 7 days of data ( 0-6), where i want the data for the new day like this:

```auto
for (let i = 0; i < msg.payload.length; i++) {
    const el = msg.payload[i];
    const entries = Object.entries(el).map(([key, value]) => ({ topic: key, payload: value }));
    node.send([entries]);
}

```

The result is 7 messages, but no way to split them to process only the last message in next step

How do i set an topic number (or other key 1-7) for each message or an function for only grabbing msg.payload[6] in the function?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [10 December 2021 06:40 UTC](https://discourse.nodered.org/t/splitting-an-array-of-7-and-setting-an-topic-for-each-message/54980/2 "2021-12-10T06:40:25Z")

</div>

You could, but it depends how much work you are prepared to do. 😉

And as you have written your own node to do it, then you just do what would otherwise be done outside the given `split` node in that node.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 December 2021 08:45 UTC](https://discourse.nodered.org/t/splitting-an-array-of-7-and-setting-an-topic-for-each-message/54980/3 "2021-12-10T08:45:42Z")

</div>

Shouldn't be to difficult, but what is the structure of the incoming message, and how do you want the structure of the returned messages?

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [10 December 2021 11:32 UTC](https://discourse.nodered.org/t/splitting-an-array-of-7-and-setting-an-topic-for-each-message/54980/4 "2021-12-10T11:32:34Z")

</div>

Just take the split node - and with a second switch node - you filter the last message with msg.part.index = 6

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [10 December 2021 13:19 UTC](https://discourse.nodered.org/t/splitting-an-array-of-7-and-setting-an-topic-for-each-message/54980/5 "2021-12-10T13:19:04Z")

</div>

If you only want the last one - why bother iterate over the array ? just send the last one in the first place ???

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [10 December 2021 18:49 UTC](https://discourse.nodered.org/t/splitting-an-array-of-7-and-setting-an-topic-for-each-message/54980/6 "2021-12-10T18:49:25Z")

</div>

> [@monsivar](#):
>
> How do i set an topic number (or other key 1-7) for each message or an function for only grabbing msg.payload[6] in the function?

If you mean how can you request one element by using a topic then you can send the payload through a change node to set`msg.payload[msg.topic]` to `msg.payload`

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

![KisYj94CSK](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55912aed1f7a8235fb451107117f058cd5238339.gif)

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [10 December 2021 19:34 UTC](https://discourse.nodered.org/t/splitting-an-array-of-7-and-setting-an-topic-for-each-message/54980/7 "2021-12-10T19:34:43Z")

</div>

Not sure exactly what you are after but you could do;

As already suggested

```auto
const el = msg.payload[6]
const entry = Object.entries(el).map(([key, value]) => ({ topic: key, payload: value }));

return msg

```

or

```auto
for (let i = 0; i < msg.payload.length; i++) {
    const el = msg.payload[i];
    const entries = Object.entries(el).map(([key, value]) => ({ topic: `${key} index ${i}`, payload: value }));
    node.send([entries]);
}

```

This would give a topic of "key 1" which can be used to select the required msg; or

```auto
for (let i = 0; i < msg.payload.length; i++) {
    const el = msg.payload[i];
    const entries = Object.entries(el).map(([key, value]) => ({ topic: key, payload: value }));

    if (i === 6) node.send([entries]);
}

```

or add a bit to the msg `({ topic: key, payload: value })` becomes `({ topic: key, payload: value, index: i })`

Any of these options would allow easy selection of msg 6

---

<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:** [8 February 2022 19:35 UTC](https://discourse.nodered.org/t/splitting-an-array-of-7-and-setting-an-topic-for-each-message/54980/8 "2022-02-08T19:35:36Z")

</div>

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