How to Merge 4 Arrays Into Array of Objects?

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 :

[
   "0x4275696c6420546865204368616e6765210000000000000000000000000000000000",
   "0x4f6e6520536d616c6c204368696d6520666f722048756d616e697479210000000000"
]

Array 2 txids :

[
   "0xf85e3ec5c3aa06e3c960f137c94caf5af282005edd2f05ab3253d3c70141569d",
   "0x5b9f832db5ecc6024368ee134abc94c78324731d7f79f6491c1cd420bddf9633"
]

Array 3 senders :

[
   "SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M",
   "SP1TA84JTP4YRFWBK7PYKBA33H3YB60XP654RAR7M"
]

Array 4 times :

[
   "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:

[
   {
      "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!

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 ?

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

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

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

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

[{"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":[]}]
1 Like

Holy crap you are smart.

I'll wait to mark a solution but I'm literally over here like
image

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

[{"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":[]}]
1 Like

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

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

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

You could do the conversion in the same function

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;
1 Like

WOW!
You are a literal hero to me!

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