我有以下javascript
var filtered = [];
$('#footballCheck').on('change', function() {
if ($('#footballCheck').is(':checked')) {
for (var i = 0; i < fa_names.length; i++) {
if (fa_names[i]["facility_activity"].toLowerCase().indexOf("football") >= 0) {
filtered.push(fa_names[i]);
}
}
filtered.sort(SortByName);
$('#mCSB_1_container').empty(".facility-name");
for (var k = 0; k < filtered.length; k++) {
$('#mCSB_1_container').append('<div class="row facility-name">\
<button class="btn btn-default btn-fill btn-menu" date-name="' + filtered[k]["facility_name"] + '">' + filtered[k]["facility_name"] + '</button></div>');
$('#facilities-body').mCustomScrollbar("update");
}
} else {
for (var j = 0; j < filtered.length; j++) {
if (filtered[j]["facility_activity"].toLowerCase().indexOf("football") >= 0) {
delete filtered[j];
}
}
console.log(filtered);
console.log(filtered.length);
if (filtered.length > 0) {
filtered.sort(SortByName);
$('#mCSB_1_container').empty(".facility-name");
for (var k = 0; k < filtered.length; k++) {
$('#mCSB_1_container').append('<div class="row facility-name">\
<button class="btn btn-default btn-fill btn-menu" date-name="' + filtered[k]["facility_name"] + '">' + filtered[k]["facility_name"] + '</button></div>');
$('#facilities-body').mCustomScrollbar("update");
}
} else {
$('#mCSB_1_container').empty(".facility-name");
for (k = 0; k < fa_names.length; k++) {
$('#mCSB_1_container').append('<div class="row facility-name">\
<button class="btn btn-default btn-fill btn-menu" date-name="' + fa_names[k]["facility_name"] + '">' + fa_names[k]["facility_name"] + '</button></div>');
$('#facilities-body').mCustomScrollbar("update");
}
}
}
});
应该执行以下操作:
filtered
数组filtered
数组中删除其键值为&#34; facility_activity&#34;的所有对象。包含&#34; football&#34; filtered
数组为空,则附加父数组中的所有项目(未过滤的项目)这是fiddle。
背景:复选框充当搜索的过滤器,filtered
数组存储这些过滤的值(除了那个小提琴之外的其他地方,filtered
数组尚未更改)。< / p>
我的问题是console.log(filtered)
返回[]
而console.log(filtered.length)
不会返回0
而应该返回{{1}}。为什么会发生这种情况,我该如何解决?