构建基本的 JavaScript 对象数组
Building basic javascript object array
我不确定是因为很晚还是什么,但今晚我似乎很难思考如何构建一个非常基本的对象数组并构建它。
我正在尝试做的是收集文本字段 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);
};
如果有人能帮我解决这个问题,我将不胜感激。提前谢谢。
在这里你去工作小提琴
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);
};
假设你想要这样的输出,
group = {groupA:["year","make","model","trim"] , groupB:["body","color","transmission"]},
你可以做到,
var group = {};
if (!group.hasOwnProperty(groupId)) {
group[groupId] = [];
}
group[groupId].push(spec.id);
相关文章:
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 在Javascript中转换对象数组
- 在JavaScript中通过索引从对象数组中获取值
- Backbone虹吸以获取对象数组
- 如何在DataTables 2.1中迭代对象数组
- Javascript-根据赋值顺序,按键合并对象数组
- 将事件附加到对象/数组
- jQuery$.inArray()总是返回-1和一个对象数组
- javascript处理一个对象数组以获得一个新的对象数组
- javascript在数组中获取对象数组中键的所有不同值
- 在对象数组中查找多个值的d3范围
- Undercore.js获取对象数组中键对象的值
- 在mongoose中使用正则表达式在对象数组中进行查询搜索
- 如何通过json对象数组为嵌套对象赋值
- 如何循环通过2个对象数组并通过匹配id进行合并
- 为对象数组创建列表项
- 如何使用javascript合并两个对象数组
- JSON到对象数组,并向每个对象添加项
- JavaScript:从对象数组中获取唯一值及其计数
- 按不同项目对对象数组进行排序