# Accessing Data from object that is in array(arrays again in object)

**URL:** <https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861>\
**Category:** General\
**Tags:** javascript\
**Created:** [29 December 2022 04:55 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861 "2022-12-29T04:55:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jhankhana](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jhankhana](https://discourse.nodered.org/u/Jhankhana)\
**Post date:** [29 December 2022 04:55 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/1 "2022-12-29T04:55:26Z")

</div>

Hello,  
I am using http request to get data of one month. Screenshots of what I am getting in debug is attached below which is array of objects. Now I want to access minvalue from the first object and maxvalue from last object. But I am unable to write the function node for it.  
Please help me with this.

```auto
[{"id":"46b3ab03.36e904","type":"ui_button","z":"88215ed2.1446d","name":"","group":"ee1c959c.6ac4f8","order":0,"width":0,"singleBrowser":false,"height":0,"passthru":false,"label":"This Month","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","powerMode":false,"x":290,"y":760,"wires":[["4a655ce9.ac06d4"]]},{"id":"4a655ce9.ac06d4","type":"http request","z":"88215ed2.1446d","name":"","method":"GET","ret":"obj","url":"","timeout":"","mindspherePath":"/api/iottsaggregates/v4/aggregates?assetId=ded1a0bfec43400f970e566f8263c6d3&aspectName=BatchMix&from=2022-12-01T00:00:00.000Z&to=2022-12-07T00:00:00.000Z&intervalUnit=hour&intervalValue=12&select=Agg1_Current_Weight","useMindsphereAuth":true,"isAdmin":false,"secretHeaders":"","x":530,"y":760,"wires":[["dc97188a.f2fe58","aacbcb9e.ef42a8"]]},{"id":"aacbcb9e.ef42a8","type":"function","z":"88215ed2.1446d","name":"","func":"const agg1 = msg.payload[0];\nconst minvalue = agg1.Agg1_Total_Weight.minvalue;\n//const maxvalue = agg1.maxvalue;\n//const difference = maxvalue - minvalue;\nmsg.payload = minvalue;\nreturn msg;\n//return { payload: { minvalue, maxvalue, difference } };","outputs":1,"language":"javascript","noerr":0,"x":740,"y":760,"wires":[["b8e08189.44e68"]],"_type":"node"},{"id":"b8e08189.44e68","type":"ui_text","z":"88215ed2.1446d","group":"ee1c959c.6ac4f8","order":0,"width":0,"singleBrowser":false,"height":0,"name":"","label":"Agg1","format":"{{msg.payload}}","layout":"row-left","x":930,"y":760,"wires":[]},{"id":"dc97188a.f2fe58","type":"debug","z":"88215ed2.1446d","name":"","active":true,"console":"false","xaxis":"_time","complete":"false","x":630,"y":880,"wires":[]},{"id":"ee1c959c.6ac4f8","type":"ui_group","z":"","name":"This Month","tab":"975e5c7e.bc29e","order":4,"disp":true,"width":"6","collapse":false,"_type":"config"},{"id":"975e5c7e.bc29e","type":"ui_tab","z":"","name":"Test Plant","icon":"dashboard","publicAccess":"true","fmAccess":"none","fmAccessType":"private","assetType":"","asset":"","assetName":"","assetText":"","image":"","description":"","hideTitleBar":false,"_type":"config"}]

```

![Screenshot (119)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/062ac155ca7904371499ef3f5ca4c20e5e86e704.png)

 ![Screenshot (120)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/ae1bebb74e35ece1886630e0e6793219e844fa6d.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [29 December 2022 05:21 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/2 "2022-12-29T05:21:20Z")

</div>

Just like your other topic, the message arrives as `msg` and not as `msg.payload` (how did you accomplish this?)

if there is no `.payload`:

`const agg1 = msg.payload[0]` this does not work.

---

<div class="post-metadata">

**Author:** ![Jhankhana](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jhankhana](https://discourse.nodered.org/u/Jhankhana)\
**Post date:** [29 December 2022 05:30 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/3 "2022-12-29T05:30:21Z")

</div>

No.  
It works with msg.payload.

Yesterday, Solution (by @tve) which worked is as below

```auto
const agg1 = msg.payload[0].Agg1_Total_Weight;
const minvalue = agg1.minvalue;
const maxvalue = agg1.maxvalue;
const difference = maxvalue - minvalue;
msg.payload = difference;
return msg;

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [29 December 2022 05:30 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/4 "2022-12-29T05:30:52Z")

</div>

I refer to your screenshot.

---

<div class="post-metadata">

**Author:** ![Jhankhana](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jhankhana](https://discourse.nodered.org/u/Jhankhana)\
**Post date:** [29 December 2022 05:47 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/5 "2022-12-29T05:47:44Z")

</div>

Yes but, I think it represents msg.payload only.  
That soluation works for array of single object but gives error for more then one objects. I think I have missed something. I guess here It's object in arrays and arrays are again in one object.  
Error: Cannot read properties of undefined (reading 'Agg1\_Total\_Weight') (at position 1:49)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [29 December 2022 05:53 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/6 "2022-12-29T05:53:40Z")

</div>

> Yes but, I think it represents msg.payload only.

It states: `msg: Object`  
and the properties live directly under it. There is no `payload`.

---

<div class="post-metadata">

**Author:** ![Jhankhana](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jhankhana](https://discourse.nodered.org/u/Jhankhana)\
**Post date:** [29 December 2022 10:56 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/7 "2022-12-29T10:56:25Z")

</div>

So I should write  
const agg1 = msg[0].Agg1\_Total\_Weight; instead of const agg1 = msg.payload[0].Agg1\_Total\_Weight;  
do I understand correct?

---

<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:** [29 December 2022 11:09 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/8 "2022-12-29T11:09:11Z")

</div>

The debug panel is your friend here.

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![Jhankhana](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jhankhana](https://discourse.nodered.org/u/Jhankhana)\
**Post date:** [30 December 2022 04:56 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/9 "2022-12-30T04:56:46Z")

</div>

I am getting 1st object from the object of arrays. But now not able to get the of particular member of that object (For exp maxvalue minvalue....).  
Please help me with this!

[flows (7).json](https://discourse.nodered.org/uploads/short-url/qKM1nCetpXCskmLKTgScBwWqs4K.json) (2.3 KB)  
p

 ![Screenshot (125)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1b1cb93b1a1b1074fa0f0f339af1cf6a80bce38e.png)  
 ![Screenshot (126)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c21e9b4371bad79f59b856e92f090020b76cdfcf.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [30 December 2022 05:56 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/10 "2022-12-30T05:56:29Z")

</div>

Where do you get the data from, via an http request node ?  
If yes, the aggregates array can never end up like msg.aggregates without some manual manipulation.

---

<div class="post-metadata">

**Author:** ![Jhankhana](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jhankhana](https://discourse.nodered.org/u/Jhankhana)\
**Post date:** [30 December 2022 06:01 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/11 "2022-12-30T06:01:06Z")

</div>

yes. via http request node.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [30 December 2022 06:02 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/12 "2022-12-30T06:02:12Z")

</div>

That is not possible in standard node-red.  
Then again, I see you posted a screenshot of a flow that does not look like a standard node-red installation.

Did you go over the documentation that @jbudd linked to and explained ?

---

<div class="post-metadata">

**Author:** ![Jhankhana](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jhankhana](https://discourse.nodered.org/u/Jhankhana)\
**Post date:** [30 December 2022 11:16 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/13 "2022-12-30T11:16:39Z")

</div>

It's working!  
Well, it was quite easy. Sorry for bothering and thank you for patience and help.

```auto
const agg1 = msg.payload.aggregates[0].Agg1_Total_Weight.minvalue;
msg.payload = agg1;
return msg;

```

---

<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 February 2023 11:17 UTC](https://discourse.nodered.org/t/accessing-data-from-object-that-is-in-array-arrays-again-in-object/72861/14 "2023-02-28T11:17:11Z")

</div>

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