# Manipulating Data form Global Object

**URL:** https://discourse.nodered.org/t/manipulating-data-form-global-object/31206
**Category:** General
**Created:** [6 August 2020 07:54 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206 "2020-08-06T07:54:02Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thomas](https://discourse.nodered.org/u/thomas)
#### Post date: [6 August 2020 07:54 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/1 "2020-08-06T07:54:03Z")

</div>

Hello everyone,

I have got a strange issue.  
I would like to take data (array) from a global variable object. Replace the null values with another value and send the result to another node by msg.payload. Sounds easy.  
BUT when I get the data from the variable it seems like it's not possible to change the values. The nulls are still in the array. When I create the same data in another variable by hand everything works just fine. When I just get the data from the variable and save it to another variable. It doesn't work out with the new variable either.  
I also tried to get the data with RED.util.cloneMessage() but it doesn't change the outcome.  
The variable containing the data I want to manipulate is changed all the time. Maybe there is a correlation.

Thank you for your help.

Sincerely Thomas

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [6 August 2020 08:16 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/2 "2020-08-06T08:16:09Z")

</div>

can you show us the flow doing this? Export it and add it to a reply

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [6 August 2020 08:20 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/3 "2020-08-06T08:20:07Z")

</div>

(You beat me to the post)

---

<div class="post-metadata">

### Author: ![thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thomas](https://discourse.nodered.org/u/thomas)
#### Post date: [6 August 2020 08:42 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/4 "2020-08-06T08:42:55Z")

</div>

The project is kind of complex and needs multiple csv files to work. But I will try to recreat the problem in a simple flow.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [6 August 2020 08:47 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/5 "2020-08-06T08:47:45Z")

</div>

Show us the function you are using, with the cloneMessage() call included. It does sound as if your modified object is being overwritten when the global array is updated.  
Use the `</>` button and paste the function in where it says.

---

<div class="post-metadata">

### Author: ![thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thomas](https://discourse.nodered.org/u/thomas)
#### Post date: [6 August 2020 08:53 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/6 "2020-08-06T08:53:11Z")

</div>

```auto
var data_ist = RED.util.cloneMessage(global.get("prozesstemp_global_object_2").ist_verlauf[2]);

for (let i in data_ist){
    if (data_ist[i] === null){
        if (i >= 1){
            if (data_ist[i-1] !== null){
            var lower_value = data_ist[i-1];
            data_ist[i] = lower_value ;
            }
            
        }
    }
}

msg.payload = data_ist;
return msg;

```

---

<div class="post-metadata">

### Author: ![thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thomas](https://discourse.nodered.org/u/thomas)
#### Post date: [6 August 2020 09:28 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/7 "2020-08-06T09:28:14Z")

</div>

```auto
[{"id":"35096b3b.a84954","type":"inject","z":"dd2b1b78.857558","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":580,"y":400,"wires":[["71a5b799.900de8"]]},{"id":"b71d41f5.a5bf6","type":"debug","z":"dd2b1b78.857558","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":990,"y":400,"wires":[]},{"id":"71a5b799.900de8","type":"function","z":"dd2b1b78.857558","name":"remove null","func":"var data_ist = RED.util.cloneMessage(global.get(\"prozesstemp_global_object_3\").ist_verlauf[2]);\n//var data_ist_original = RED.util.cloneMessage(global.get(\"prozesstemp_global_object\").ist_verlauf[2]);\nvar msg2= {};\n\nfor (let i in data_ist){\n if (data_ist[i] === null){\n if (i >= 1){\n if (data_ist[i-1] !== null){\n var lower_value = data_ist[i-1];\n data_ist[i] = lower_value ;\n }\n \n }\n }\n}\n\n/*for (let i in data_ist_original){\n if (data_ist_original[i] === null){\n if (i >= 1){\n if (data_ist_original[i-1] !== null){\n lower_value = data_ist_original[i-1];\n data_ist_original[i] = lower_value ;\n }\n \n }\n }\n}*/\n\nmsg.payload = data_ist;\n//msg2.payload = data_ist_original;\nreturn [msg,msg2];","outputs":2,"noerr":0,"x":770,"y":400,"wires":[["b71d41f5.a5bf6"],["478a382a.0a8808"]]},{"id":"ff633fc3.02bc3","type":"debug","z":"dd2b1b78.857558","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":990,"y":340,"wires":[]},{"id":"717741a1.ff4d4","type":"function","z":"dd2b1b78.857558","name":"create object","func":"function getRandomInt(min, max) {\n min = Math.ceil(min);\n max = Math.floor(max);\n return Math.floor(Math.random() * (max - min)) + min;\n}\n\nvar prozesstemp_global_object_3 = global.get(\"prozesstemp_global_object_3\")||{\n liste_maschinen_ID : [],\n timestamp_verlauf : [],\n soll_verlauf :[],\n max_verlauf :[],\n min_verlauf :[],\n ist_verlauf :[[],[],[]],\n timestamp_on:[],\n status: [],\n prozessnummer:[],\n filenames: [],\n};\n\nvar value_in = getRandomInt(0, 10);\n\nif (value_in <= 3){\n value_in = null;\n}\n\n\nvar ist_data = prozesstemp_global_object_3.ist_verlauf[2];\n\nist_data.push(value_in);\n\n if(ist_data.length >= 20){\n ist_data.pop();\n }\n \nprozesstemp_global_object_3.ist_verlauf[2] = ist_data;\n\nglobal.set(\"prozesstemp_global_object_3\",prozesstemp_global_object_3);\n\nmsg.payload = ist_data;\n\nreturn msg;","outputs":1,"noerr":0,"x":770,"y":340,"wires":[["ff633fc3.02bc3"]]},{"id":"206b9e82.73ef82","type":"inject","z":"dd2b1b78.857558","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":590,"y":340,"wires":[["717741a1.ff4d4"]]},{"id":"478a382a.0a8808","type":"debug","z":"dd2b1b78.857558","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":990,"y":440,"wires":[]}]

```

So I recreated the Situation but I work just fine with the new object. I don't know why...

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [6 August 2020 09:29 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/8 "2020-08-06T09:29:50Z")

</div>

That looks ok. If you feed that directly into a debug node does it show the replaced data?  
If not then add this line after `data_ist[i] = lower_value ;`

```auto
node.warn(`Setting data_ist[${i}] to ${lower_value}`)

```

and see what it says in the debug pane  
I am not sure whether for/in is a recommended way of iterating an array, but it does seem to work for a simple array. It is normally used for iterating properties.

**[Edit]** Just had a thought, if the elements you are testing for null don't exist at all in the array then I don't think for/in will select them, so perhaps that is the cause.

---

<div class="post-metadata">

### Author: ![thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thomas](https://discourse.nodered.org/u/thomas)
#### Post date: [6 August 2020 09:34 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/9 "2020-08-06T09:34:52Z")

</div>

That's a great tip! I don't think they do exist. How can I catch them anyway?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [6 August 2020 09:43 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/10 "2020-08-06T09:43:20Z")

</div>

Start by using `for (i = 0; i < data_ist.length ; i++) { ` which will give you an iteration even if the element doesn't exist, then I think that rather than using `=== null` it might be better with `== null` as that, I believe, will also catch undefined.  
Add the warn as I suggested then you can check it is working.

As a general debugging point, when you have something not working it is always best to check which bits are working and which bits are not before jumping to a conclusion about what is happening, so add debug along the chain to find the first point of failure.

---

<div class="post-metadata">

### Author: ![thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thomas](https://discourse.nodered.org/u/thomas)
#### Post date: [6 August 2020 09:49 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/11 "2020-08-06T09:49:27Z")

</div>

Thank you very much! Not knowing how exactly a for in loop works cost me at least 5 hours of my life....

---

<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: [20 August 2020 09:49 UTC](https://discourse.nodered.org/t/manipulating-data-form-global-object/31206/12 "2020-08-20T09:49:30Z")

</div>

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