1

Im building a "Diagnostic" view in my JS based app.

I load all of the localstorage in to a variable:

var diagnosticinformation = localStorage;

And then the idea is that I can output diagnosticinformation in to a text box:

textarea1.value = JSON.stringify(diagnosticinformation);

This all works fine.

However, before outputting the diagnosticinformation I would like to hide userToken and sharedpasscode for security purposes.

diagnosticinformation['userToken']="HIDDEN";
diagnosticinformation['sharedpasscode']="HIDDEN";

However, when i do this, the LocalStorage element is then also updated so userToken then become "HIDDEN" and the sharedpasscode also becomes "HIDDEN" in the actual localstorage of the browser, not just within diagnosticinformation

Does anyone know why this is happening?

Surely if object1 = object2 - I can alter object1 without it affecting object2 - however this isn't the case.

PaulF
  • 124
  • 1
  • 8
  • `diagnosticinformation` is just a *reference* to `localStorage`....not a copy of it – charlietfl Oct 11 '20 at 15:15
  • Clone the `localStorage` content with `var newMap = {}; for (var i in localStorage) newMap[i] = localStorage[i];` – Guerric P Oct 11 '20 at 15:19
  • When you do `var diagnosticinformation = localStorage;` (try to use const and let) you are not copying the object, you are creating a reference to it so if you update `localStorage` or `diagnosticinformation` then both objects will be updated as both are "connected". You can use `Object.assign({}, localStorage)` to define `diagnosticinformation` – Runsis Oct 11 '20 at 15:23

0 Answers0