1

I'm trying to learn javascript and I'm trying to figure out local storage for a project. But first I believe I need to programmatically build the keys from my Team constructor.

 var Team = function(team, manager, cell, add1, add2, city, st, zip, sponsor) {
  this.team = team;
  this.manager = manager;
  this.cell = cell;
  this.add1 = add1;
  this.add2 = add2;
  this.city = city;
  this.st = st;
  this.zip = zip;
  this.sponsor = sponsor;
  };

This is from a form I've built and now I want to build the localstorage. This is my failed attempt:

 function saveTeam() {
 for (var i = 0; i < Team.length; i++) {
 localStorage["league." + i "." + javascriptNameOfProperty ] = $('#'+ javascriptNameOfPropertyName + ').val() };

Or something like that. I've tried "property", "key" and others I've tried don't work for the javascriptNameofProperty. Of course, once I get that figured then I've got to figure out localstorage, of course.

sam452
  • 1,281
  • 3
  • 20
  • 33
  • Perhaps you are looking for something like this: http://stackoverflow.com/questions/921789/how-to-loop-through-javascript-object-literal-with-objects-as-members. The question is specifically about objects within objects, but if you strip out the inner for-loop of the chosen answer's example, you'll get something useful I think. – AlexMA Jul 31 '12 at 19:42
  • http://stackoverflow.com/questions/1007981/how-to-get-function-parameter-names-values-dynamically-from-javascript – trumank Jul 31 '12 at 19:50

3 Answers3

2

In this case, you could use an object literal instead of a constructor (I don't see any reason for the constructor on the code you posted, and you should be instantiating the object with new, which you aren't). Consider using this (assuming all variables you were passing to Team are already defined):

var team = {
    team : team,
    manager : manager,
    cell : cell,
    add1 : add1,
    add2 : add2,
    city : city,
    st : st,
    zip : zip,
    sponsor : sponsor
}

This can be iterated with the following code:

for(var key in team) {
    localStorage["league." + key] = team[key];
}

I think this is not doing exactly what your original code was trying to do, but it is not clear if you have multiple teams, how they are created, and how they're used. I hope this will help.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
1

Team is a function, not an array.

I will assume that you do have an array of Teams.

You need to use a for in loop:

var teams = [ ... ];

for (var i = 0; i < teams.length; i++) {
    for (var key in team) {
        localStorage["league." + i + "." + key] = $('#' + key).val()
    }
}
trumank
  • 2,704
  • 3
  • 22
  • 31
1

To build a little off everyone else you can also do it this way.

var Team = function(team, manager, cell, add1, add2, city, st, zip, sponsor) {
  this.team = team;
  this.manager = manager;
  this.cell = cell;
  this.add1 = add1;
  this.add2 = add2;
  this.city = city;
  this.st = st;
  this.zip = zip;
  this.sponsor = sponsor;
};

Team.prototype.save = function () { 
  for ( var prop in this )
  { 
    if (this.hasOwnProperty(prop))
      console.log('%s => %s', prop, this[prop]);
      // your localStorage saving logic goes here
  }
};

var t = new Team('vteam','vmanager','vcell','vadd1','vadd2','vcity','vst','vzip','vsponors');

t.save();

This will save only the properties of the Team object (anything defined inside the team function this.prop.

millimoose
  • 39,073
  • 9
  • 82
  • 134
travis
  • 8,055
  • 1
  • 17
  • 19