# How to Merge 4 Arrays Into Array of Objects?

**URL:** <https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462>\
**Category:** General\
**Created:** [31 August 2021 21:05 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462 "2021-08-31T21:05:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![geoseph](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/geoseph/32/39526_2.png) [@geoseph](https://discourse.nodered.org/u/geoseph)\
**Post date:** [31 August 2021 21:05 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/1 "2021-08-31T21:05:40Z")

</div>

Hey everyone!  
So once again my pea brain is failing me lol.  
I'm trying to figure out how to merge 4 Arrays into an Array of Objects  
(each of the Arrays has up to 20 strings inside it).  
Here are a sample of the Arrays I'm attempting to merge (only with 2 Strings per to save space)

**Array 1** `memos` :

```auto
[
   "0x4275696c6420546865204368616e6765210000000000000000000000000000000000",
   "0x4f6e6520536d616c6c204368696d6520666f722048756d616e697479210000000000"
]

```

**Array 2** `txids` :

```auto
[
   "0xf85e3ec5c3aa06e3c960f137c94caf5af282005edd2f05ab3253d3c70141569d",
   "0x5b9f832db5ecc6024368ee134abc94c78324731d7f79f6491c1cd420bddf9633"
]

```

**Array 3** `senders` :

```auto
[
   "SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M",
   "SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M"
]

```

**Array 4** `times` :

```auto
[
   "2021-08-30T02:31:02.000Z",
   "2021-08-30T00:25:29.000Z"
]

```

I've tried to use the Join node but End results never turn out the way I need them to be.

**I want the final output to be an Array of Objects** (obviously upto 20 objects) looking something like:

```auto
[
   {
      "memo":"0x4275696c6420546865204368616e6765210000000000000000000000000000000000",
      "txid":"0xf85e3ec5c3aa06e3c960f137c94caf5af282005edd2f05ab3253d3c70141569d",
      "sender":"SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M",
      "time":"2021-08-30T02:31:02.000Z"
   },
   {
      "memo":"0x4f6e6520536d616c6c204368696d6520666f722048756d616e697479210000000000",
      "txid":"0x5b9f832db5ecc6024368ee134abc94c78324731d7f79f6491c1cd420bddf9633",
      "sender":"SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M",
      "time":"2021-08-30T00:25:29.000Z"
   }
]

```

If anyone has the skills to help get me sorted out I'd really appreciate it!  
Thanks!

---

<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:** [31 August 2021 21:13 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/2 "2021-08-31T21:13:17Z")

</div>

> [@geoseph](#):
>
> I've tried to use the Join node but End results never turn out the way I need them to be

Do these 4 arrays arrive as different msgs at different times ?  
if yes .. what differentiates the msg of being memos, txids, senders and times. its topic ?

---

<div class="post-metadata">

**Author:** ![geoseph](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/geoseph/32/39526_2.png) [@geoseph](https://discourse.nodered.org/u/geoseph)\
**Post date:** [31 August 2021 21:22 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/3 "2021-08-31T21:22:44Z")

</div>

I have an initial json output of Array of Objects, in `msg.payload.results`  
that I pass thru 4 different function nodes at the same time:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea2b730ccf728cc9e15ecda845ad8a77bdb61c5f.png)

Here is a copy of the memos isolating function, named "remap array (of memos)":

```auto
var rawArray = msg.payload.results; //pass in the initial Array of Objects
var memos = rawArray.map((obj) => {
    return obj.token_transfer.memo.toString()
});

msg.payload = memos; //pass out the new Array of just memos
msg.topic = "Chimes";
return msg;

```

So I guess "No" to your question?  
eg to isolate out the properties I care about to change format for example then merge the final results into the needed structure of Array of Objects.

* * *

Overall Objective here is to take my original array of objects, and clean up the values, for example the memo is in hex not ascii like it needs to be (I already have a separate function that I wrote to convert from hex to ascii but I'm having troubles trying to apply it to the specific property of all objects in the initial Array of Objects). So i figured breaking them out into individual Array of strings I could more easily update the values and merge them back together after the fact.  
I've tried to leverage map() method too, but damn I'm dumb lol

---

<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:** [31 August 2021 21:29 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/4 "2021-08-31T21:29:54Z")

</div>

There must be a way to merge the javascript logic into one function but here's a test flow and see if it helps.

```auto
[{"id":"d9a3a084ae41e588","type":"inject","z":"4895ea10b4ee9ead","name":"memos","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"memos","payload":"[\"0x4275696c6420546865204368616e6765210000000000000000000000000000000000\", \"0x4f6e6520536d616c6c204368696d6520666f722048756d616e697479210000000000\"]","payloadType":"json","x":290,"y":940,"wires":[["ea470a2e72c1e2b8"]]},{"id":"05ecace6423a77f3","type":"inject","z":"4895ea10b4ee9ead","name":"txids","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"txids","payload":"[\"0xf85e3ec5c3aa06e3c960f137c94caf5af282005edd2f05ab3253d3c70141569d\", \"0x5b9f832db5ecc6024368ee134abc94c78324731d7f79f6491c1cd420bddf9633\"]","payloadType":"json","x":290,"y":1000,"wires":[["ea470a2e72c1e2b8"]]},{"id":"4f7a2f2133b3c246","type":"inject","z":"4895ea10b4ee9ead","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"senders","payload":"[\"SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M\", \"SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M\"]","payloadType":"json","x":290,"y":1060,"wires":[["ea470a2e72c1e2b8"]]},{"id":"48beb9c57910ca7c","type":"inject","z":"4895ea10b4ee9ead","name":"times","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"times","payload":"[\"2021-08-30T02:31:02.000Z\", \"2021-08-30T00:25:29.000Z\"]","payloadType":"json","x":290,"y":1120,"wires":[["ea470a2e72c1e2b8"]]},{"id":"ea470a2e72c1e2b8","type":"join","z":"4895ea10b4ee9ead","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":520,"y":1040,"wires":[["c91392da11f6fe49","0d2565e899cacc3e"]]},{"id":"41b6a95980bd672e","type":"debug","z":"4895ea10b4ee9ead","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":1040,"wires":[]},{"id":"c91392da11f6fe49","type":"function","z":"4895ea10b4ee9ead","name":"","func":"let data = msg.payload\nlet result = []\n\n\ndata.memos.forEach((el, index) => {\n\n result.push(\n {\n \"memo\": data.memos[index],\n \"txid\": data.senders[index],\n \"sender\": data.txids[index],\n \"time\": data.times[index]\n }\n )\n\n})\n\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":1040,"wires":[["41b6a95980bd672e"]]},{"id":"0d2565e899cacc3e","type":"debug","z":"4895ea10b4ee9ead","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":620,"y":960,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![geoseph](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/geoseph/32/39526_2.png) [@geoseph](https://discourse.nodered.org/u/geoseph)\
**Post date:** [31 August 2021 21:32 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/5 "2021-08-31T21:32:59Z")

</div>

> [@UnborN](#):
>
> ```auto
> [{"id":"d9a3a084ae41e588","type":"inject","z":"4895ea10b4ee9ead","name":"memos","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"memos","payload":"[\"0x4275696c6420546865204368616e6765210000000000000000000000000000000000\", \"0x4f6e6520536d616c6c204368696d6520666f722048756d616e697479210000000000\"]","payloadType":"json","x":290,"y":940,"wires":[["ea470a2e72c1e2b8"]]},{"id":"05ecace6423a77f3","type":"inject","z":"4895ea10b4ee9ead","name":"txids","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"txids","payload":"[\"0xf85e3ec5c3aa06e3c960f137c94caf5af282005edd2f05ab3253d3c70141569d\", \"0x5b9f832db5ecc6024368ee134abc94c78324731d7f79f6491c1cd420bddf9633\"]","payloadType":"json","x":290,"y":1000,"wires":[["ea470a2e72c1e2b8"]]},{"id":"4f7a2f2133b3c246","type":"inject","z":"4895ea10b4ee9ead","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"senders","payload":"[\"SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M\", \"SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M\"]","payloadType":"json","x":290,"y":1060,"wires":[["ea470a2e72c1e2b8"]]},{"id":"48beb9c57910ca7c","type":"inject","z":"4895ea10b4ee9ead","name":"times","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"times","payload":"[\"2021-08-30T02:31:02.000Z\", \"2021-08-30T00:25:29.000Z\"]","payloadType":"json","x":290,"y":1120,"wires":[["ea470a2e72c1e2b8"]]},{"id":"ea470a2e72c1e2b8","type":"join","z":"4895ea10b4ee9ead","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":520,"y":1040,"wires":[["c91392da11f6fe49","0d2565e899cacc3e"]]},{"id":"41b6a95980bd672e","type":"debug","z":"4895ea10b4ee9ead","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":870,"y":1040,"wires":[]},{"id":"c91392da11f6fe49","type":"function","z":"4895ea10b4ee9ead","name":"","func":"let data = msg.payload\nlet result = []\n\n\ndata.memos.forEach((el, index) => {\n\n result.push(\n {\n \"memo\": data.memos[index],\n \"txid\": data.senders[index],\n \"sender\": data.txids[index],\n \"time\": data.times[index]\n }\n )\n\n})\n\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":1040,"wires":[["41b6a95980bd672e"]]},{"id":"0d2565e899cacc3e","type":"debug","z":"4895ea10b4ee9ead","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":620,"y":960,"wires":[]}]
> 
> ```

Holy crap you are smart.  
 ![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cdc0ede6efd37a935826cf1cc221679ddbc07c76.gif)

I'll wait to mark a solution but I'm literally over here like  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/5513899dcd871513c23162d9011d5d40b27b1fc9.jpeg)

---

<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:** [31 August 2021 21:37 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/6 "2021-08-31T21:37:38Z")

</div>

Can i see your head explode.  
here's similar except the topics become the object property names, and is semi dynamic, as in you can feed it 4,5,,6,nth arrays of nth sizes

```auto
[{"id":"15705321.9a3e5d","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"memos","payload":"[\"0x4275696c6420546865204368616e6765210000000000000000000000000000000000\", \"0x4f6e6520536d616c6c204368696d6520666f722048756d616e697479210000000000\"]","payloadType":"json","x":180,"y":2900,"wires":[["3efb35e6.f24cea"]]},{"id":"3efb35e6.f24cea","type":"join","z":"c74669a0.6a34f8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":360,"y":2960,"wires":[["1a4aa8e9.04365f"]]},{"id":"6781fa74.d62d14","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"txids","payload":"[\"0xf85e3ec5c3aa06e3c960f137c94caf5af282005edd2f05ab3253d3c70141569d\", \"0x5b9f832db5ecc6024368ee134abc94c78324731d7f79f6491c1cd420bddf9633\"]","payloadType":"json","x":170,"y":2960,"wires":[["3efb35e6.f24cea"]]},{"id":"315f6ba.5ed7394","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"senders","payload":"[\"SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M\", \"SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M\"]","payloadType":"json","x":160,"y":3020,"wires":[["3efb35e6.f24cea"]]},{"id":"9a3a42de.ea00a8","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"times","payload":"[\"2021-08-30T02:31:02.000Z\", \"2021-08-30T00:25:29.000Z\"]","payloadType":"jsonata","x":160,"y":3080,"wires":[["3efb35e6.f24cea"]]},{"id":"1a4aa8e9.04365f","type":"function","z":"c74669a0.6a34f8","name":"","func":"let obj_keys = Object.keys(msg.payload);\nlet hold = msg.payload;\nmsg.payload =[];\n\nfor(let i = 0; i < hold[obj_keys[0]].length; i++){\n let parts ={};\n for(let j =0; j < obj_keys.length; j++){\n parts[obj_keys[j]] = hold[obj_keys[j]][i];\n }\n msg.payload[i] = parts;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":2960,"wires":[["54d32199.b9c458"]]},{"id":"54d32199.b9c458","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":2900,"wires":[]}]

```

---

<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:** [31 August 2021 21:38 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/7 "2021-08-31T21:38:59Z")

</div>

its a simple loop ..  
it takes as an assumption that the length of the arrays is always the same

---

<div class="post-metadata">

**Author:** ![geoseph](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/geoseph/32/39526_2.png) [@geoseph](https://discourse.nodered.org/u/geoseph)\
**Post date:** [31 August 2021 21:44 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/8 "2021-08-31T21:44:56Z")

</div>

Don't mind me while I feed off your guys table scraps mmmmmmk?

[https://media1.giphy.com/media/21GCae4djDWtP5soiY/giphy.gif(image larger than 4096KB)](https://media1.giphy.com/media/21GCae4djDWtP5soiY/giphy.gif)

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [31 August 2021 21:48 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/9 "2021-08-31T21:48:10Z")

</div>

Don't merge before checking the type. Type Dog may to bite 😃

---

<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:** [31 August 2021 22:13 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/10 "2021-08-31T22:13:13Z")

</div>

> [@geoseph](#):
>
> the memo is in hex not ascii like it needs to be

You could do the conversion in the same function

```auto
let data = msg.payload
let result = []

data.memos.forEach((el, index) => {
    result.push(
        {
            "memo": Buffer.from(data.memos[index].slice(2), "hex").toString('ascii'),
            "txid": data.txids[index],
            "sender": data.senders[index],
            "time": data.times[index]
        }
    )
})

msg.payload = result;
return msg;

```

---

<div class="post-metadata">

**Author:** ![geoseph](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/geoseph/32/39526_2.png) [@geoseph](https://discourse.nodered.org/u/geoseph)\
**Post date:** [31 August 2021 23:02 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/11 "2021-08-31T23:02:39Z")

</div>

WOW!  
You are a literal hero to me!

---

<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:** [14 September 2021 23:03 UTC](https://discourse.nodered.org/t/how-to-merge-4-arrays-into-array-of-objects/50462/12 "2021-09-14T23:03:16Z")

</div>

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