# Transpose the Object/Array

**URL:** <https://discourse.nodered.org/t/transpose-the-object-array/72170>\
**Category:** General\
**Created:** [14 December 2022 08:28 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170 "2022-12-14T08:28:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [14 December 2022 08:28 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170/1 "2022-12-14T08:28:14Z")

</div>

Hi,  
I have an output from mysql query in this format,

```auto
[{"R":"Batch Preparation Delay","D":281},{"R":"Tea - Lunch Break","D":109},{"R":"Laminate Loading","D":58}]

```

i want the output to be changed to this format

```auto
[{"Batch Preparation Delay"},{"Tea - Lunch Break"},{"Laminate Loading"}]
[{281},{109},{58}]

```

How do i do that ? i searched the forum and there was a suggestion to use map function in function node, but there was no clear instructions, i don't know javascript so please someone can help me....

---

<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:** [14 December 2022 08:44 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170/2 "2022-12-14T08:44:27Z")

</div>

> [@smanjunath211](#):
>
> i want the output to be changed to this format
> 
> ```auto
> [{"Batch Preparation Delay"},{"Tea - Lunch Break"},{"Laminate Loading"}]
> [{281},{109},{58}]
> 
> ```

Not possible unless it it a string.

That is not valid JS or JSON.

---

<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:** [14 December 2022 08:45 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170/3 "2022-12-14T08:45:16Z")

</div>

The output from mysql is an array of objects. Each object has name : value pairs.

The format you say you want is invalid because it doesn't have name:value pairs, just values.

So do you want arrays  
[281, 109, 58]  
Or arrays of objects?  
[{D:281}, {D:109},{D:58}]

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [14 December 2022 09:23 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170/4 "2022-12-14T09:23:14Z")

</div>

i want to feed the output to a timeplot graph in flexdash, and i think it requires the format to be in  
below format, i am not sure what this format is, since i dont speak this 'language'

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

i think it is the one without the curly braces...

```auto
[["Batch Preparation Delay","Tea - Lunch Break","Laminate Loading"}]]
[[281],[109],[58]]

```

---

<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:** [14 December 2022 09:31 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170/5 "2022-12-14T09:31:29Z")

</div>

Try setting msg.labels to JSONata J: `[$$.payload.R]`  
and then msg.payload to JSONata J: `[[$$.payload.D]]` or `[$$.payload.D.[$]]` in a change node.  
You have to set msg.labels first, as if you set payload the data will be overwrtten.

---

<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:** [14 December 2022 09:34 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170/6 "2022-12-14T09:34:48Z")

</div>

I don't know about charts in Flexdash.

Since I am also a bit uncertain how to use map() I've written a function using your data to explain it to myself. It creates arrays and for comparison, an object. I hope it's useful.

```auto
const source=[{ "R": "Batch Preparation Delay", "D": 281 }, { "R": "Tea - Lunch Break", "D": 109 }, { "R": "Laminate Loading", "D": 58 }]

// The map() function calls another function for each element of an array.
// It returns another array

// Two different ways to use map() to transform the input array
// Firstly Define a function to return one array element D value
function getDValues(element) {
    return element.D
}
// Call that function for each array element
const Darr = source.map(getDValues)

//Exactly the same thing for R values but expressed more compactly
const Rarr = source.map(e => { return e.R })

// The compact form for D values
//const Darr = source.map(e => { return e.D })

// to create an array of objects
const Robj = source.map(e => { return { R: e.R } })

msg.payload = {R:Rarr, D:Darr, Robj: Robj}
return msg;

```

ps You can always rely on @E1cid to provide a Jsonata answer, sadly I can never grasp why it works! 😃

---

<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:** [14 December 2022 09:43 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170/7 "2022-12-14T09:43:16Z")

</div>

> [@jbudd](#):
>
> I can never grasp why it works! 😃

Silicon Pixies.

[edit] They run all over payload, mapping and returning all elements with property D in an array.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [14 December 2022 09:50 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170/8 "2022-12-14T09:50:02Z")

</div>

Perfect !. I got the required format. now I will feed that into the chart and see, how it works.

I also got the same result by modifying my mysql query, got two separate queries to output the results into two different arrays and then merged them with a join node, and got the same result as your method

thanks !

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [14 December 2022 09:52 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170/9 "2022-12-14T09:52:17Z")

</div>

Will give this a try @E1cid 's solution worked for me. and i also got a workaround by having two separate queries and then merging them with join node. (not a neat solution, but works)

---

<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:** [28 December 2022 09:52 UTC](https://discourse.nodered.org/t/transpose-the-object-array/72170/10 "2022-12-28T09:52:51Z")

</div>

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