Reputation: 7788
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.
Upvotes: 0
Views: 56
Reputation: 6025
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);
};
Upvotes: 2
Reputation: 15742
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);
Upvotes: 1