How to convert payload object to array of elements

I'm not sure if the title correctly dictates what i need and i'm sorry if this something simple to do but i have no experience with javascript or coding and i'm fairly new to node red.

Here's an example of what i'm trying to accomplish,
I have the following payload:

{
   "code":[
       "352",
       "353",
       "354"
   ],
   "number":[
       447.1,
       674.14,
       493.78
   ],
   "date":[
       "31/3/21",
       "1/4/21",
       "6/4/21"
   ]
}

I would like to convert it to

[
   {
       "code":"352",
       "number":447.1,
       "date":"31/3/21"
   },
   {
       "code":"353",
       "number":674.14,
       "date":"1/4/21"
   },
   {
       "code":"354",
       "number":493.78,
       "date":"6/4/21"
   }

 ]

Is there a node that can do this?

This might not be the most efficient but it does the trick. In a function node:

const x = {
   "code":[
       "352",
       "353",
       "354"
   ],
   "number":[
       447.1,
       674.14,
       493.78
   ],
   "date":[
       "31/3/21",
       "1/4/21",
       "6/4/21"
   ]
}

const out = []

Object.keys(x).forEach( entry => {
    const e = {}
    
    e.code = x[entry][0]
    e.number = x[entry][1]
    e.date = x[entry][2]
    
    out.push(e)
})

msg.payload = out
return msg

Thank you,
This is in the right direction but not exactly what i need.

the result got me:

[
   {
       "code":"352",
       "number":"353",
       "date":"354"
   },
   {
       "code":447.1,
       "number":674.14,
       "date":493.78
   },
   {
       "code":"31/3/21",
       "number":"1/4/21",
       "date":"6/4/21"
   }
]

The elements in the array get the values from the same original object.

Oops, so much for trying to rush. You will need to pick it apart a bit further then. All the elements are there, just not quite in the right order! :scream:

After 5 hours of searching the web, i finally found a way!

this is a link to the solution in case anyone ever needs it!

1 Like

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