# Create Objects from List object (global context)

**URL:** <https://discourse.nodered.org/t/create-objects-from-list-object-global-context/84672>\
**Category:** General\
**Tags:** function-node\
**Created:** [18 January 2024 05:38 UTC](https://discourse.nodered.org/t/create-objects-from-list-object-global-context/84672 "2024-01-18T05:38:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick.Nogara](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@Patrick.Nogara](https://discourse.nodered.org/u/Patrick.Nogara)\
**Post date:** [18 January 2024 05:38 UTC](https://discourse.nodered.org/t/create-objects-from-list-object-global-context/84672/1 "2024-01-18T05:38:19Z")

</div>

Trying to create a list of Mixers objects in the Global Context and set the MixerID property as defined in List object. Objects are defined as:

```auto
//Mixer Object definition
    var MixerUDT = {
                    "MixerID" : "",
                     "Speed_SP":0, 
                     "Speed_PV":0, 
                     "Direction":0, 
                     "Comms_OK":1, 
                     "Comms_Sent_ID":0,
                     "Comms_Retires":0,
                     "Comms_Errors" :0 
                     }

    //List of Mixer to create including their ID
    var MixerList = {
                     "M01": "Mixer01",
                     "M02": "Mixer02" 
                     }

```

Looking for help with code to iterate through MixerList to create mixers M01 & M02 and set their MixerIDs to "Mixer01" and "Mixer02" respectively...Alternatively, help debugging any one of the approaches I've tried below:

All approaches I've tried create Mixers object with correct names, but the MixerID for all mixers is the same: "Mixer02". Cannot for the life of me work out why.  
Suspect it has something to do with not understanding when pass-by-references versus pass-by-value is occurring!

**APPROACH 1:**

```auto
//For each Key in MixerList create a MixerUDT and set MixerID
    for ( let KeyName in MixerList) {
        MixerUDT.MixerID = MixerList[KeyName]; //set MixerID in UDT 
        global.set(KeyName.toString(),MixerUDT); //create instance in global context

        //For Diagnostics
        msg.payload = MixerUDT; //In each iteration I see MixerUDT has been correctly updated
        node.send(msg);
    }

```

**APPROACH 2:**

```auto
 //For each Key in MixerList create a MixerUDT and set MixerID
    for ( let KeyName in MixerList) {
        global.set(KeyName.toString(),MixerUDT); //create instance of default UDT

        let _Mixer = global.get(KeyName.toString()); //get reference to global variable?!
        _Mixer["MixerID"] = MixerList[KeyName]; //change the MixerID

        //For Diagnostics
        msg.payload = _Mixer; //In each iteration I see _Mixer has been correct updated
        node.send(msg);
        }

```

Thanks in advance.

---

<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:** [18 January 2024 06:12 UTC](https://discourse.nodered.org/t/create-objects-from-list-object-global-context/84672/3 "2024-01-18T06:12:20Z")

</div>

> [@Patrick.Nogara](#):
>
> `global.set(KeyName.toString(),MixerUDT); //create instance in global context`

You are setting references to the same object each time. You need to clone it.

Try

```auto
const clone= {...MixerUDT}
clone.name = KeyName
global.set(KeyName.toString(), clone)

```

---

<div class="post-metadata">

**Author:** ![Patrick.Nogara](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@Patrick.Nogara](https://discourse.nodered.org/u/Patrick.Nogara)\
**Post date:** [18 January 2024 06:24 UTC](https://discourse.nodered.org/t/create-objects-from-list-object-global-context/84672/4 "2024-01-18T06:24:55Z")

</div>

Thanks for helping out a NOOB, Steve.

Is the line `const clone= {...MixerUDT}` pseudo code?!  
I'm not familiar with "{..."

Also, I should use:

```auto
clone.MixerID = KeyName;

```

---

<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:** [18 January 2024 06:30 UTC](https://discourse.nodered.org/t/create-objects-from-list-object-global-context/84672/5 "2024-01-18T06:30:14Z")

</div>

> [@Patrick.Nogara](#):
>
> Is the line `const clone= {...MixerUDT}` pseudo code?!

No. This is JavaScript. The `...` is the spread (or rest) operator. It works on arrays and objects.

> **[3 Ways to Clone Objects in JavaScript | SamanthaMing.com](https://www.samanthaming.com/tidbits/70-3-ways-to-clone-objects/)**
>
> Because objects in JavaScript are references values, you can't simply just copy using the =. Here are 3 ways for you to clone an object...

---

<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:** [18 January 2024 06:50 UTC](https://discourse.nodered.org/t/create-objects-from-list-object-global-context/84672/6 "2024-01-18T06:50:46Z")

</div>

> [@Patrick.Nogara](#):
>
> respectively...Alternatively, help debugging any one of the approaches I've tried below:

If you need to debug you can use `node.warn` for point in time values.

E.g.

```auto
node.warn({clone})

```

This will output to the node-red debug sidebar

---

<div class="post-metadata">

**Author:** ![Patrick.Nogara](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@Patrick.Nogara](https://discourse.nodered.org/u/Patrick.Nogara)\
**Post date:** [18 January 2024 07:50 UTC](https://discourse.nodered.org/t/create-objects-from-list-object-global-context/84672/7 "2024-01-18T07:50:58Z")

</div>

You're a champ. Thank you.

---

<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:** [18 March 2024 07:51 UTC](https://discourse.nodered.org/t/create-objects-from-list-object-global-context/84672/8 "2024-03-18T07:51:02Z")

</div>

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