我是javascript和jquery的新手所以请耐心等待。我需要使用以下结构创建数组的动态哈希
{
"cricket_fields": ["aniket", "shivam" ],
"soccer_fields": ["tiwari", "sandeep", "abhijeet" ]
"football_fields": ["varun", "shikhar","naveen","sudheer","apaar"],
}
我的代码问题是每当
var fields = {};
$.each( $(BUILDER.selected_field_items), function(key,value ){
fields[$(this).data('group')] = [$(this).data('label')]
});
$(BUILDER.selected_field_items) =
[div.field_items, div.field_items, div.field_items, div.field_items, div.field_items, div.field_items, div.field_items, div.field_items, div.field_items, div.field_items, div.field_items, div.field_items, prevObject: n.fn.init(1), context: document, selector: "#selected_fields_container div.field_items"]
当迭代这个对象时,我找到了($(this).data(' group'))和带有$(this).data(' label' )
div.field_items =
"<div class="field_items" data-label="aniket" data-index="0" data-group="cricket_fields">Aniket <span class="plus-icon action-icon">+</span> <i class="icon-cross2 action-icon"></i> </div>"
我已经显示了特定div.fields_items
的数据答案 0 :(得分:1)
我不知道$(BUILDER.selected_field_items)
是什么,但这应该达到你想要的目的:
if (key in fields) {
// push `value` into the array if the key is found
fields[key].push(value)
} else {
// create an array with the first index set to `value` and set it to fields[key]
fields[key] = [value]
}