1

In my project I need to access objects properties and nested objects properties and save them to localStorage, and be able to retrieve them individually and replace the current values with the stored ones, but I don't know how to automate that with (for example) a for-in loop.

The saving part works well, but as you can see, i'm quite stuck with the load part. I thought about accessing the property with window["propertyNameAsSring"], but i learned that you can only access global vars and not their properties through the window object.

Tell me if you need any clarification !

var files = {
  paramsToSave: {
    "game_pageID": game.pageID,
    "game_page_sceneID": game.page.sceneID,
    "events": Events,
    "time_hours": time.hours,
    "time_minutes": time.minutes,
    "time_daysPlayed": time.daysPlayed,
  },
  save() {
    for (param in this.paramsToSave) {
      localStorage[`save_${param}`] = JSON.stringify(this.paramsToSave[param]);
    }
  },
  load() {
    // ?
    // window[this.paramsToSave[param]] = JSON.parse(localStorage[`save_${param}`]);
  }
};

var game = {

..

}

var time = {

..

}

var Events = {

..

}

4
  • 1
    Why not this.paramsToSave[param] = JSON.parse(...) ? Commented Nov 28, 2018 at 17:56
  • @Jonas Because it then saves to "files.paramToSave["game_pageID"]" and not to the actual "game.pageID" in the "game" object Commented Nov 28, 2018 at 18:00
  • Wuy not just game.pageID = getStored("pageID"); then? Commented Nov 28, 2018 at 18:03
  • Because right now i'm trying this with 6 variables, but i'll need it to scale a lot, thus i need something like a for loop to go saving/loading all the variables.. Commented Nov 28, 2018 at 18:11

1 Answer 1

1

If you just want to load params, you'll still need some structure in your files object that has a list of paramsToSave. This will serve as the schema of properties that may be serialized. You don't have to provide initial values for them, but you do need to specify their storage paths.

After this point, you'll probably want to exchange information between the paramsToSave structure and the actual global objects.

If you want to do that, then you probably just want to write getters and setters for properties of the paramsToSave.

var game = { pageID: 'abc123', page: { sceneID: 'sce234' } }; // example
var Events = { placeholder: 'This is the events object' }; // example
var time = { hours: 1, minutes: 2, daysPlayed: 3 }; // example
let mock_localStorage = {}; // mock

var files = {
   
  paramsToSave: {
    get game_pageID() { return game.pageID; },
    set game_pageID(value) { game.pageID = value; },
    
    get game_page_sceneID() { return game.page.sceneID; },
    set game_page_sceneID(value) { game.page.sceneID = value; },
    
    get events() { return Events; },
    set events(value) { Events = value; },
    
    get time_hours() { return time.hours; },
    set time_hours(value) { time.hours = value; },
    
    get time_minutes() { return time.minutes; },
    set time_minutes(value) { time.minutes = value; },
    
    get time_daysPlayed() { return time.daysPlayed; },
    set time_daysPlayed(value) { time.daysPlayed = value; }
  },
  save() {
    for (let param in this.paramsToSave) {
      mock_localStorage[`save_${param}`] = JSON.stringify(this.paramsToSave[param]);
    }
  },
  load() {
    for (let param in this.paramsToSave) {
      this.paramsToSave[param] = JSON.parse(mock_localStorage[`save_${param}`]);
    }
  }
};

files.save();
console.log(mock_localStorage);

// Simulate changing something in localStorage
mock_localStorage[`save_${'time_daysPlayed'}`] = '365';

files.load();
console.log(JSON.stringify(time, null, 2));

Sign up to request clarification or add additional context in comments.

1 Comment

Oooh this is perfect ! I never understood the use of getters and setters, but now I see when they can be useful ! Thanks a lot !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.