0

我不确定是因为太晚了还是什么,但今晚我似乎很难通过构造一个非常基本的对象数组并构建它来思考。

我要做的是收集文本字段ID列表并将它们放入组变量中;

例子

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

我不确定如何构建我的主要组对象。我不确定它是否应该使用数组。下面是我的第一次尝试

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

我尝试像这样构建我的组对象,但我真的觉得我做错了。

  //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);
};

如果有人能帮我解决这一切,我将不胜感激。提前致谢。

4

2 回答 2

2

在这里你去工作小提琴

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);
};
于 2013-09-22T05:06:36.270 回答
1

假设你想要这样的输出,

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

你可以做,

var group = {};

if (!group.hasOwnProperty(groupId)) {            
    group[groupId] = [];            
}        
group[groupId].push(spec.id);
于 2013-09-22T05:06:25.147 回答