# Javascript in general: Dynamic Object Literals

**URL:** <https://discourse.nodered.org/t/javascript-in-general-dynamic-object-literals/17563>\
**Category:** General\
**Created:** [5 November 2019 19:43 UTC](https://discourse.nodered.org/t/javascript-in-general-dynamic-object-literals/17563 "2019-11-05T19:43:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bohtho](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bohtho/32/6153_2.png) [@bohtho](https://discourse.nodered.org/u/bohtho)\
**Post date:** [5 November 2019 19:43 UTC](https://discourse.nodered.org/t/javascript-in-general-dynamic-object-literals/17563/1 "2019-11-05T19:43:38Z")

</div>

I'm way above my head here in javascript but why does this not put neither the "tidsPunkt" nor the msg.payload into rommet.profil[dag] ?

```auto
    if (rommet.hasOwnProperty("profil") === false) {
        rommet.profil = {};
    }

    var returnedObject = Object.assign(rommet.profil, {[dag]: {[tidsPunkt]: msg.payload}});

```

Norwegian dictionary:  
Rommet = the room  
Dag = day  
Tidspunkt = time  
Profil = profile

Via an RBE node I’m trying to save every new setpoint of several rooms in a profile object from another (KNX RF) system. (I’m also rounding the times to nearest quarter of an hour).

Why? Because I’m going to ramp the collection of points according to room heating coefficient so my main system is not taken by surprise each time a setpoint suddenly jumps or falls.

---

<div class="post-metadata">

**Author:** ![bohtho](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bohtho/32/6153_2.png) [@bohtho](https://discourse.nodered.org/u/bohtho)\
**Post date:** [5 November 2019 21:22 UTC](https://discourse.nodered.org/t/javascript-in-general-dynamic-object-literals/17563/2 "2019-11-05T21:22:27Z")

</div>

Added a check for the nested 'dag' (Day) property too. I really thought Object.assign would merge (Add object) if nested differently as well.

---

<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:** [5 November 2019 21:59 UTC](https://discourse.nodered.org/t/javascript-in-general-dynamic-object-literals/17563/3 "2019-11-05T21:59:16Z")

</div>

Where does `rommet` come from ?

To add a new object (i assume that is what you want, not sure as there is context missing) you don't need to use Object.assign. Object.assign wil clone the object, unless that is what you want.

There is a node to emulate a [ramping thermostat.](https://www.npmjs.com/package/node-red-contrib-ramp-thermostat) that includes hysteresis, perhaps that could help ?

---

<div class="post-metadata">

**Author:** ![bohtho](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bohtho/32/6153_2.png) [@bohtho](https://discourse.nodered.org/u/bohtho)\
**Post date:** [5 November 2019 22:22 UTC](https://discourse.nodered.org/t/javascript-in-general-dynamic-object-literals/17563/4 "2019-11-05T22:22:03Z")

</div>

`rommet` is copied from part of the nested context data.

I guess Object.assign is not needed (certainly not the cloning part) as it didn't do what I thought with regards to merging. Went back to `rommet.profil[dag][tidsPunkt] = msg.payload` after checking for existence of all levels of nesting.

I am actually going to use that contrib-node, but I want to set the profiles dynamically from that other heating system (on a weekly delay obviously).

---

<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:** [5 November 2019 22:49 UTC](https://discourse.nodered.org/t/javascript-in-general-dynamic-object-literals/17563/5 "2019-11-05T22:49:09Z")

</div>

> but I want to set the profiles dynamically

It supports dynamic profiles.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [6 November 2019 05:37 UTC](https://discourse.nodered.org/t/javascript-in-general-dynamic-object-literals/17563/6 "2019-11-06T05:37:13Z")

</div>

Regardless whether you have used this approach or not... for the sake of completeness:

In your code example, you need to remove the `[]` brackets, then it will work as expected.

```auto
Object.assign(rommet.profil, {dag: {tidsPunkt: msg.payload}});

```

And note that the return value of `Object.assign()` is the object in `rommet.profil`, not `rommet`.

---

<div class="post-metadata">

**Author:** ![bohtho](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bohtho/32/6153_2.png) [@bohtho](https://discourse.nodered.org/u/bohtho)\
**Post date:** [6 November 2019 07:03 UTC](https://discourse.nodered.org/t/javascript-in-general-dynamic-object-literals/17563/7 "2019-11-06T07:03:07Z")

</div>

> [@bakman2](#):
>
> It supports dynamic profiles.

Yes, I know. I garble my sentences in English sometimes. I meant that I want automatically retrieve and dynamically store new setpoints in different daily profiles per room in that node vs just manually copying over setpoints to that node.
