# Struggling with appending an object to an array of objects to display in ui-table

**URL:** <https://discourse.nodered.org/t/struggling-with-appending-an-object-to-an-array-of-objects-to-display-in-ui-table/96771>\
**Category:** Dashboard\
**Tags:** function-node\
**Created:** [26 April 2025 17:25 UTC](https://discourse.nodered.org/t/struggling-with-appending-an-object-to-an-array-of-objects-to-display-in-ui-table/96771 "2025-04-26T17:25:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pjg142000](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pjg142000/32/100431_2.png) [@pjg142000](https://discourse.nodered.org/u/pjg142000)\
**Post date:** [26 April 2025 17:25 UTC](https://discourse.nodered.org/t/struggling-with-appending-an-object-to-an-array-of-objects-to-display-in-ui-table/96771/1 "2025-04-26T17:25:03Z")

</div>

I've successfully implemented several flows and dashboard and now want to use output from the openweathermap (ow) api to calculate the forecast yield of a PV roof collector. I'm copying several properties from ow's output into an object out\_el. After that I try to append the objects content to an array of obejcts. And here it goes wrong. While the "node.waarn(out\_el) object prints the right content, the array "out\_arr" only lists 24 times the csame ontent of the LAST object. I'm struggling to understand this after trying to replace out\_array[3\*i+ii)=out\_el with push and unshift.

 ![js code](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/4/8443d164c98f8316c94aeea97cc40db86f25f488.png)  
 ![output from node.warn(out_el)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e92b05f95546f46498351e364ad2cd6a33d011a.png)

---

<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:** [26 April 2025 17:49 UTC](https://discourse.nodered.org/t/struggling-with-appending-an-object-to-an-array-of-objects-to-display-in-ui-table/96771/2 "2025-04-26T17:49:49Z")

</div>

Move `var out_el` (line 42) to inside the inner `for` loop (line 51)

---

<div class="post-metadata">

**Author:** ![pjg142000](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pjg142000/32/100431_2.png) [@pjg142000](https://discourse.nodered.org/u/pjg142000)\
**Post date:** [26 April 2025 18:07 UTC](https://discourse.nodered.org/t/struggling-with-appending-an-object-to-an-array-of-objects-to-display-in-ui-table/96771/3 "2025-04-26T18:07:56Z")

</div>

You're a hero, tnx so much. I was not expecting the for-loop to influence the scope of the variable.

---

<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:** [26 April 2025 18:15 UTC](https://discourse.nodered.org/t/struggling-with-appending-an-object-to-an-array-of-objects-to-display-in-ui-table/96771/4 "2025-04-26T18:15:20Z")

</div>

It has nothing to do with the loop _really_. More to do with block scope and how JS sets references (pointer) to the object.

Because you created `out_el` at the top of the code, one time (kinda global scope), when you set it into `out_arr[x]` you are really only setting a pointer to the original/singular `out_el` object.

In essence, every element of `out_arr` pointed to the SINGLE `out_el` object that you were modifying in each iteration of the loop.

By moving the var `out_el` to inside the loops `block scope`, it generates a brand new (and more importantly a separate) object (for each iteration) within the scope of the for loops block.

Hope that helps you understand better and avoid future issues.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [29 April 2025 01:26 UTC](https://discourse.nodered.org/t/struggling-with-appending-an-object-to-an-array-of-objects-to-display-in-ui-table/96771/5 "2025-04-29T01:26:10Z")

</div>

Can i suggest to you that you might find Solcast more accurate for predictions of Solar PV than just using the OWM Irradiance numbers

I have tried both and for my system it is chalk and cheese

Craig

---

<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:** [13 May 2025 01:26 UTC](https://discourse.nodered.org/t/struggling-with-appending-an-object-to-an-array-of-objects-to-display-in-ui-table/96771/6 "2025-05-13T01:26:53Z")

</div>

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