Linking variables

This a simple summary of my code:

var list = {
    "group":{
        "subgroup1":[{"name1":"Jimmy","name2":"Bob"}],
        "subgroup2":[{"name1":"Sarah","name2":"Nick"},{"name1":"Kevin","name2":"George"}]
    }
}
function group(group,name){
    var linktothegroup;

    //Note: In my actual code it will attempt to join a group before creating one this just creates a new one


    //group: Specifies which subgroup to join (my actual code will check if it's valid this is just a simpler version
    //name: the users name

    list["group"][group].push({"name1":name,"name2":""});
    linktothegroup = someway to link that group I just added; //I figured I could find the array slot and specify but that will change if a group is deleted

    this.leavegroup = function(){
        //leaves the group
        //uses the linktothegroup var to be removed from the group
    }
    this.changename = function(name){
        linktothegroup.name1 = name;
        return true;
    }
}
var cubscouts = new group("subgroup1","Billy");
cubscouts.changename("Fred");

I just wanna be able to edit values in the "subgroupX" fields (Basically for the changename function), but there are groups leaving and joining all the time so I can't specify the array slot in a variable.

So basically is there a way to edit one variable and have another variable change with it?

How about something like this?:

var groups = {

  joinGroup: function(groupName, memberName) {
    if(!this[groupName]) {  // create the group if it doesn't exist
      this[groupName] = []  // each group is just an array of names
    }
    var group = this[groupName]
    group.push(memberName)

    // returns an object allowing for the member's name to be changed later
    return {
      // this function becomes a closure, so the group and memberName variables
      // retain their values from the enclosing scope
      changeName: function(newMemberName) {
        group.splice(group.indexOf(memberName), 1, newMemberName)
      }
    }
  }
}

Which allows for this usage:

myCubscoutMembership = groups.joinGroup('cubscouts', 'Billy')
myCubscoutMembership.changeName('Bob')

The key piece of the puzzle (if i understand your question correctly) is that the changeName function is returned as a closure around the group and memberName variables - so even when changeName is called at a later time, the correct group and old name are remembered.

To keep the answer focused I've left out the leaveGroup() method but that can be added to the returned object as another function. I'm also not addressing what would happen if a whole group is removed but that depends on what makes sense for your app.

A couple of simplifications from your original sample:

  1. I flattened the top-level list var which seemed to contain only one group.
  2. I simplified each group to be an array of names instead of a hash, i.e. ['billy', 'bob'] instead of { name1: 'billy', name2: 'bob' }.

Hope some of this helps.

You can store the object in a variable that you reference in the functions.

function group(group,name){
    //other work here
    var linktothegroup = {"name1":name,"name2":""};
    list["group"][group].push(linktothegroup);

    this.leavegroup = function(){
        var groups = list["group"][group];
        for(var i = 0; i < groups.length; i++) {
            if(groups[i] === linktothegroup) {
                groups.splice(i, 1);
                break;
            }
        }
    }
    this.changename = function(name){
        linktothegroup.name1 = name;
        return true;
    }
}