# How to access msg object per position

**URL:** https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950
**Category:** Dashboard
**Created:** [20 August 2021 09:37 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950 "2021-08-20T09:37:53Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [20 August 2021 09:37 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/1 "2021-08-20T09:37:54Z")

</div>

Hello,

I have a payload that looks that way:

payload: array[8]  
0: object

- Feldkennung: 104101
- Feldname: "Projektnummer"
- WertimFeld: "123 dummy"

I would like to access the values without knowledge of the payload object.

with `msg.payload[x].Feldname` i can get one spezific value.

I would like to have something like that: `msg.payload[x].[y]`

I have searched for a solution but i didnt find one.

maybe someone has a clue?

Dearly  
Chorum

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 August 2021 10:04 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/2 "2021-08-20T10:04:49Z")

</div>

If y is a variable containing the name then you are nearly there, just take the dot out.  
`msg.payload[x][y]`

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [20 August 2021 11:21 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/3 "2021-08-20T11:21:13Z")

</div>

Thats my Problem, the names could change (are unknown/different amounts of variables) and thats why i would like to use the position.

---

<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: [20 August 2021 11:25 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/4 "2021-08-20T11:25:01Z")

</div>

If you don't know what the payload is how do you expect to use the values?

In other words, What are you doing with the data?

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [20 August 2021 11:42 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/5 "2021-08-20T11:42:07Z")

</div>

Its the answer of an "dynamic" sql query. I ask for Objectdata and get answers from a pool of different datasets.  
for instance: Objecttype Tire (diamater) , Objecttype Table (H,W,D/ amount tablelegs/ color ...)

So different amount of fields and different names.

The Answer is a list of Datasets with the same search criteria.

On the list i use checkboxes to filter the objects i want to use.  
After that i put them on a html document for printing.

But i only need an access to the values without valuenames. 🙂  
Like with an array...

Atm i build a solution with asking for all the names to use them. But i hope there is a simplier solution.

---

<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: [20 August 2021 11:54 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/6 "2021-08-20T11:54:33Z")

</div>

> [@Chorum](#):
>
> But i only need an access to the values without valuenames. 🙂  
> Like with an array

Then you need to map the results and grab the key values.

Look up `map` and `Object.values()` on the mdn website.

Something like below in a function node after the SQL node...

```auto
msg.payload = msg.payload.map(e=> {
  return [...Object.values(e)]
})
return msg;

```

_(Untested/written on phone)_

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [20 August 2021 11:58 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/7 "2021-08-20T11:58:14Z")

</div>

Thanks !  
Didnt work with map till today. I will try and will respond after that.

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [20 August 2021 12:09 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/8 "2021-08-20T12:09:31Z")

</div>

Works like a charm, thank you!

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [23 August 2021 09:43 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/9 "2021-08-23T09:43:38Z")

</div>

Hello,

i need to go a little bit more further...

With :  
`werte = msg.payload.map(e=> { return [...Object.values(e)] })`

And:  
`namen = msg.payload.map(e=> { return [... Object.keys(e)] }) `

i have the keys and values in different maps.

now i would like to get both in one map, so i can loop through it and get keys and values.

i tried some versions of merging the maps but nothing worked.

may be creating a new map out of the two ?

For an example of the wished structure :

From

payload: array[8]  
0: object

```
Feldkennung: 104101
Feldname: "Projektnummer"
WertimFeld: "123 dummy"

```

too

payload: array[8]  
0: object

```
0: 104101
1: "Projektnummer"
2: "123 dummy"
3: "Feldkennung"
4: "Feldname"
5: "WertimFeld"
```

---

<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: [23 August 2021 10:27 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/10 "2021-08-23T10:27:02Z")

</div>

That seems like an odd thing to want but anyhow, it looks like you just want to join the 2 arrays.

You can easily achieve that with the spread operator...

```javascript
const werte = msg.payload.map(e=> { return [...Object.values(e)] });
const namen = msg.payload.map(e=> { return [...Object.keys(e)] });
msg.payload = [...werte, ...namen];
return msg;

```

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [23 August 2021 10:55 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/11 "2021-08-23T10:55:50Z")

</div>

The result:  
 ![mergedmap](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/1/c168cfb0958e15accc80d57d9ca2be735db77cc3.png)

i could work with that solution but may be there is a solution that works not like a push?

May be there is a simplier solution for my problem, i try to formulate my problem again.

I use a SQL query. As a result i get some datasets.  
These Datasets differ in amount of entrys, names of the entrys and the values.  
For every dataset i want to get the sructure key:value for all entrys.

if i get everything in an array with objects like:

```auto
0: 104101
1: "Projektnummer"
2: "123 dummy"
3: "Feldkennung"
4: "Feldname"
5: "WertimFeld"

```

i can get everything i want dynamicly and in loops without knowing what kind of objects i get from my sql query...

may be i think too complicated ?

---

<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: [23 August 2021 11:18 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/12 "2021-08-23T11:18:33Z")

</div>

My point was that format is terrible. An object or array of objects is far more suited.

Try these 3 demos - see what suits you...

```auto
[
    {
        "id": "eb890c42660627b1",
        "type": "inject",
        "z": "eb3bd3ff.33367",
        "name": "fake DB data",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[{\"Feldkennung\":104101,\"Feldname\":\"Projektnummer\",\"WertimFeld\":\"123 dummy\"},{\"Feldkennung\":104101,\"Feldname\":\"Projektnummer\",\"WertimFeld\":\"123 dummy\",\"AnotherFeld\":\"another value\"}]",
        "payloadType": "json",
        "x": 920,
        "y": 660,
        "wires": [
            [
                "7eb4c0d1f00403b5",
                "883b851d53d857a2",
                "33aead0e2507b653"
            ]
        ]
    },
    {
        "id": "7eb4c0d1f00403b5",
        "type": "function",
        "z": "eb3bd3ff.33367",
        "name": "what you are after",
        "func": "const werte = msg.payload.map(e => { return [...Object.values(e), ...Object.keys(e)] });\nmsg.payload = werte.flat(2);\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1150,
        "y": 600,
        "wires": [
            [
                "8e518250ae8f4e2f"
            ]
        ]
    },
    {
        "id": "8e518250ae8f4e2f",
        "type": "debug",
        "z": "eb3bd3ff.33367",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1510,
        "y": 600,
        "wires": []
    },
    {
        "id": "883b851d53d857a2",
        "type": "function",
        "z": "eb3bd3ff.33367",
        "name": "a better way",
        "func": "const werte = msg.payload.map(e => Object.entries(e));\nmsg.payload = werte.flat(1);\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1130,
        "y": 660,
        "wires": [
            [
                "38e4234858c1392e"
            ]
        ]
    },
    {
        "id": "33aead0e2507b653",
        "type": "function",
        "z": "eb3bd3ff.33367",
        "name": "much better (keeps name+value together)",
        "func": "const data = msg.payload;\nconst result = [];\nlet rowNo = 0;\ndata.forEach(d => {\n rowNo++;\n let nvarr = Object.entries(d);\n nvarr.forEach(e => {\n const nvpair = {\n row: rowNo,\n field: e[0],\n value: e[1]\n }\n result.push(nvpair)\n })\n})\nmsg.payload = result;\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1220,
        "y": 720,
        "wires": [
            [
                "fd54add26e56f888"
            ]
        ]
    },
    {
        "id": "38e4234858c1392e",
        "type": "debug",
        "z": "eb3bd3ff.33367",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1510,
        "y": 660,
        "wires": []
    },
    {
        "id": "fd54add26e56f888",
        "type": "debug",
        "z": "eb3bd3ff.33367",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1510,
        "y": 720,
        "wires": []
    }
]

```

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [23 August 2021 12:33 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/13 "2021-08-23T12:33:03Z")

</div>

i will try to understand the code and will respond after i tried that.

thanks!

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [23 August 2021 12:46 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/14 "2021-08-23T12:46:17Z")

</div>

On demo 1 & 2 i get:"werte.flat is not an function". I cant see whats wrong there.  
But i learned the method .flat 🙂

Demo 3 works and seems to be the solution.

I will try that solution and will respond after that. May be tomorrow.

Thanks for your time!

---

<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: [23 August 2021 14:36 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/15 "2021-08-23T14:36:54Z")

</div>

> [@Chorum](#):
>
> On demo 1 & 2 i get:"werte.flat is not an function"

You must be on node-js V10 or below. [compatibility report](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat#browser_compatibility)

I would definitely recommend upgrading as that is EOL / out of support.

V14 LTS (or even V16) is now preferable.

---

<div class="post-metadata">

### Author: ![Chorum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chorum/32/47644_2.png) [@Chorum](https://discourse.nodered.org/u/Chorum)
#### Post date: [2 September 2021 11:50 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/16 "2021-09-02T11:50:11Z")

</div>

I needed to use an other option, but i learned something new.

Thanks!

---

<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: [16 September 2021 11:50 UTC](https://discourse.nodered.org/t/how-to-access-msg-object-per-position/49950/17 "2021-09-16T11:50:16Z")

</div>

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