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.
Move var out_el
(line 42) to inside the inner for
loop (line 51)
You're a hero, tnx so much. I was not expecting the for-loop to influence the scope of the variable.
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.
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