如何检查jQuery .each()是否为空

Put*_*din -3 javascript jquery

如何检查jQuery .each()函数是否找不到内容,然后将txt值更改为“默认”。

$(this).closest('.filter-select__dropdown-inner').find('> .button--checkbox.button--active').each(function(i) {
    var val = $(this).val();

    if (i == 0) {
        txt = val;
    } else {
        txt = txt + ', ' + val;
    }
});
Run Code Online (Sandbox Code Playgroud)

我已经尝试

if (i == null) {
    txt = 'default';
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用

Jay*_*oda 5

.length在jQuery中使用以获取长度

var lnt = $(this).closest('.filter-select__dropdown-inner').find('> .button--checkbox.button--active');
if(lnt.length > 0) {

lnt.each(function(i) {
    var val = $(this).val();

    if (i == 0) {
        txt = val;
    } else {
        txt = txt + ', ' + val;
    }
});

}
Run Code Online (Sandbox Code Playgroud)