当我对数组进行排序时,我在控制台中收到以下错误:
Uncaught TypeError: Cannot read property 'localeCompare' of null
到目前为止我已尝试过:
HTML:
<table class="table table table-striped table-bordered table-hover" id="userLastSyncTable">
<thead>
<tr>
<th class="sortable sortDevicePlatform orderDevicePlatformByASC">Device Platform</th>
</tr>
</thead>
<tbody></tbody>
的JavaScript / JQuery的:
var TestArray = ["iOS 7", null, "iOS 8.4", null, null, null, "iOS 9"];
ShowUserSyncTable();
function ShowUserSyncTable() {
var tableRecord = '';
// Loop through all the returned records and add them to select box
for (var i = 0; i < TestArray.length; i++) {
tableRecord += "<tr id=" + "" + "><td>" + TestArray[i] + "</td></tr>";
}
$('#userLastSyncTable').find('tbody').html(tableRecord);
}
$(".sortDevicePlatform").click(function () {
var clickedDevicePlatformSorting = $(this).hasClass('orderDevicePlatformByASC') ? 'orderDevicePlatformByDESC' : 'orderDevicePlatformByASC';
$('.sortDevicePlatform').removeClass('orderDevicePlatformByASC').removeClass('orderDevicePlatformByDESC');
$('.sortDevicePlatform').addClass(clickedDevicePlatformSorting);
// Sort the sync list based on device platform
TestArray.sort(function (a, b) {
if (!a) {
// Change this values if you want to put `null` values at the end of the array
return -1;
}
if (!b) {
// Change this values if you want to put `null` values at the end of the array
return +1;
}
if (clickedDevicePlatformSorting == 'orderDevicePlatformByASC' && a) return a && b ? a.localeCompare(b) : -1;
else if (b) return a && b ? b.localeCompare(a) : 1;
});
ShowUserSyncTable();
});
如何使用空值对数组进行排序?
更新小提琴进行测试:
预计会来:
点击一下显示:
iOS 7,iOS 8.4,iOS 9,null,null,null,null
点击另一个显示:
iOS 9,iOS 8.4,iOS 7,null,null,null,null
答案 0 :(得分:6)
试试这个: Demo 。
从可点击的orderDevicePlatformByASC
中删除<th ...>
班级名称。因为,它的初始外观没有排序。用它来排序。
TestArray.sort(function (a, b) {
if (clickedDevicePlatformSorting == 'orderDevicePlatformByASC' && a)
return b ? a.localeCompare(b) : -1;
else if (b)
return a ? b.localeCompare(a) : 1;
});
答案 1 :(得分:2)
您无法在null
上调用某个功能。您的数组中有一些null
值。因此,当您尝试sort
这些值时,它会崩溃。
您可以保护您的代码以避免这种情况。
TestArray.sort(function(a, b) {
if (!a) {
// Change this values if you want to put `null` values at the end of the array
return -1;
}
if (!b) {
// Change this values if you want to put `null` values at the end of the array
return +1;
}
if (sorted)
return a.localeCompare(b);
else
return b.localeCompare(a);
});
但在我看来,你应该摆脱数组中的null
值。您可以过滤它们:
TestArray = TestArray.filter(function(val) {
return val != null;
});
答案 2 :(得分:1)
Postgres LISTEN/NOTIFY - low latency, realtime?
TestArray.sort(function(a, b)
{
if (sorted && a!=null)
return a.localeCompare(b);
else if(b!=null)
return b.localeCompare(a);
});
答案 3 :(得分:1)
尝试使用三元运算符:
var TestArray = ["iOS 7", null, "iOS 8.4", null, null, null, "iOS 9"];
var sorted = true;
var fn = function(a, b) {
if (sorted)
return a ? a.localeCompare(b) : 1;
return b ? b.localeCompare(a) : -1;
};
document.write("<pre>");
TestArray.sort(fn);
document.write(JSON.stringify(TestArray));
document.write("<br/>");
sorted = !sorted;
TestArray.sort(fn);
document.write(JSON.stringify(TestArray));
document.write("</pre>");
&#13;