0

I'm not sure if it's because it's late or what, but I seem to be having a really difficult time tonight thinking through structuring a pretty basic object array and building it.

What I'm trying to do is gather a list of text field id's and put them into a group variable;

Example

groupA:{year, make, model}
groupB:{color,body}

I'm not sure how to structure my main group object. I'm not sure if it should be using an array or not. Below is my first attempt

group = {groupA:{"year","make","model","trim"},groupB:{"body","color","transmission"}}

I attempted building my group object like so, but I really feel I'm doing it all wrong.

  //Class variable
  Var group = {}

  //this method is called for every textfield
  selectGroup = function(spec) {
    //Group id is the group the field is assigned to, example groupA, or groupB
    var groupId = spec.groupId;

    //I'm checking to see if groupId exist in group object, otherwise I add it. 
    if (!group.hasOwnProperty(groupId)) {
        var obj = {};
        obj[groupId] = [];
        group = obj;
    }
    //spec.id is the field id, example make, model
    group[groupId].push(spec.id);
};

If anybody could help me fix this all up, I'd greatly appreciate it. Thanks in advance.

7
  • 2
    Missing a colon and using the wrong array char: group = {groupA:["year","make","model","trim"],groupB:["body","color","transmission"]} Commented Sep 22, 2013 at 4:48
  • Sorry, I just wrote that out quick, I guess what I'm asking is if group should be an array of groupA, groupB etc, or if group should be an object of nested objects Commented Sep 22, 2013 at 4:50
  • If you have {} you need {"year":1998,"model":"Model A","trim":"Silver"} and you need lowercase "v" in "var" Commented Sep 22, 2013 at 4:52
  • @mplungjan your first example appears to be the config I'm looking for, is my code building that object correctly or no? Commented Sep 22, 2013 at 4:56
  • Can you make a jsFiddle.net? Commented Sep 22, 2013 at 4:57

2 Answers 2

2

Here you go working fiddle

var group = {};

//this method is called for every textfield
selectGroup = function (spec) {
    //Group id is the group the field is assigned to, example groupA, or groupB
    var groupId = spec.groupId;

    //I'm checking to see if groupId exist in group object, otherwise I add it. 
    if (!group.hasOwnProperty(groupId)) {
        group[groupId] = [];
    }
    //spec.id is the field id, example make, model
    group[groupId].push(spec.id);
};
Sign up to request clarification or add additional context in comments.

1 Comment

Excelent, thanks for taking the time to put this example together.
1

Assuming you want output like this,

group = {groupA:["year","make","model","trim"] , groupB:["body","color","transmission"]},

You can do,

var group = {};

if (!group.hasOwnProperty(groupId)) {            
    group[groupId] = [];            
}        
group[groupId].push(spec.id);

Comments

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.