在JavaScript中,你不能仅仅通过删除数组中的',,,'来解决问题。
你是指这个数组['home', 'info', '', '', '', '', 'mail', 'something', 'stuff', 'other']吗?
假设有一些空字符串,你想要将它们删除。
你可以使用一个简单的JavaScript函数:
allIDs = ["home", "info", "", "", "", "", "mail", "something", "stuff", "other"];
remove_empty_str = function(arr) {
new_array = [];
for (ii = 0, len = arr.length; ii < len; ii++) {
item = arr[ii];
if (item !== "" || item !== null || item !== (void 0)) {
new_array.push(item);
}
}
return new_array;
};
newIDs = remove_empty_str(allIDs);
alert(newIDs);
我认为在进行任何jQuery输出之前,处理数组是更好的实践。
您还可以在其他应用程序中重复使用remove_empty_str()。