Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 将命名对象添加到数组_Jquery_Object_Associative Array - Fatal编程技术网

Jquery 将命名对象添加到数组

Jquery 将命名对象添加到数组,jquery,object,associative-array,Jquery,Object,Associative Array,我正在尝试为添加到数组中的对象创建关键点 widgets [ "hats": {widget}, "shirts": {widget}, ... ... ] 当我在下面记录小部件时,控制台中不会返回任何内容,或者没有对象/小部件被添加到小部件数组中 let widgets = []; $(document).on("change",'select.filter-select, .filter-checkbox input', function(e) { const widget

我正在尝试为添加到数组中的对象创建关键点

widgets [
"hats": {widget},
"shirts": {widget},
...
...
]
当我在下面记录小部件时,控制台中不会返回任何内容,或者没有对象/小部件被添加到小部件数组中

let widgets = [];
$(document).on("change",'select.filter-select, .filter-checkbox input', function(e) {
    const widget    = {};
    const $this = $(this);

    if ( $this.hasClass('checkbox') ) {
        widget.type         = $this.data('type');
        widget.blueprint    = $this.data('blueprint');
        widget.handle       = $this.data('handle');
        widget.url          = $this.data('url');
    }
    else{
        const option        = $('option:selected', this);
        widget.type         = option.parent().data('type');
        widget.blueprint    = option.parent().data('blueprint');
        widget.handle       = option.data('handle');
        widget.url          = option.data('url');
    }

    if ( widgets.length > 0 ) {
        $.each(widgets, function (key,value) {
            if (!widgets[widget.handle]) {
                widgets[widget.handle].push(widget);
            }
        });
    }
    else {
        widgets[widget.handle].push(widget);
    }
    console.log(widgets + "\n\n");
});

widgets[widget.handle]希望widgets是一个对象

您应该直接推送到小部件

widgets.push(widget);
或者,如果希望保持结构不变,则应将小部件定义为对象

const widgets = {}
...
widgets['widget.handle'] = widget

这是可行的,但我现在如何检查是否有任何对象已添加到Widgets对象,Widgets.length>0不再有效并返回未定义?我想做的是,如果小部件已经被添加到小部件中,不要再添加它,而是删除它。如果选中复选框和unCKECKIF,只想检查小部件是否为空,以替换<代码>(WigGeStuts> 0)< /C> >,然后考虑使用这里所提到的任何方法。