[英]javascript not removing undefined objects from array
我有一个使用 JS 的页面内文本搜索,这里是:
$.fn.eoTextSearch = function(pat) {
var out = []
var textNodes = function(n) {
if (!window['Node']) {
window.Node = new Object();
Node.ELEMENT_NODE = 1;
Node.ATTRIBUTE_NODE = 2;
Node.TEXT_NODE = 3;
Node.CDATA_SECTION_NODE = 4;
Node.ENTITY_REFERENCE_NODE = 5;
Node.ENTITY_NODE = 6;
Node.PROCESSING_INSTRUCTION_NODE = 7;
Node.COMMENT_NODE = 8;
Node.DOCUMENT_NODE = 9;
Node.DOCUMENT_TYPE_NODE = 10;
Node.DOCUMENT_FRAGMENT_NODE = 11;
Node.NOTATION_NODE = 12;
}
if (n.nodeType == Node.TEXT_NODE) {
var t = typeof pat == 'string' ?
n.nodeValue.indexOf(pat) != -1 :
pat.test(n.nodeValue);
if (t) {
out.push(n.parentNode)
}
}
else {
$.each(n.childNodes, function(a, b) {
textNodes(b)
})
}
}
this.each(function() {
textNodes(this)
})
return out
};
而且我有能力隐藏表格中的列和行。 当我提交搜索并获得突出显示的结果时,在这种情况下,找到的文本节点的数组长度将为 6,但页面上只有 3 个突出显示。 当您将 output 阵列到控制台时,您会得到以下信息:
所以你得到了我期待的 3 个标签,但是你看到数组实际上是由一个[span,undefined,span,undefined,undefined,span]
组成的。 因此给了我6的长度。
<span>
<span>
<span>
[span, undefined, span, undefined, undefined, span]
我不知道为什么当我检查它们时它没有去除所有未定义的文本节点。 这是我为 function 准备的。
performTextSearch = function(currentObj){
if($.trim(currentObj.val()).length > 0){
var n = $("body").eoTextSearch($.trim(currentObj.val())),
recordTitle = "matches",
arrayRecheck = new Array(),
genericElemArray = new Array()
if(n.length == 1){
recordTitle = "match"
}
//check to see if we need to do a recount on the array length.
//if it's more than 0, then they're doing a compare and we need to strip out all of the text nodes that don't have a visible parent.
if($(".rows:checked").length > 0){
$.each(n,function(i,currElem){
if($(currElem).length != 0 && typeof currElem != 'undefined'){
if($(currElem).closest("tr").is(":visible") || $(currElem).is(":visible")){
//remove the element from the array
console.log(currElem)
arrayRecheck[i] = currElem
}
}
})
}
if(arrayRecheck.length > 0){
genericElemArray.push(arrayRecheck)
console.log(arrayRecheck)
}
else{
genericElemArray.push(n)
}
genericElemArray = genericElemArray[0]
$("#recordCount").text(genericElemArray.length + " " +recordTitle)
$(".searchResults").show()
for(var i = 0; i < genericElemArray.length; ++i){
void($(genericElemArray[i]).addClass("yellowBkgd").addClass("highLighted"))
}
}
else{
$(".highLighted").css("background","none")
}
}
如果您查看下面的代码“//check to see if we need to do a recount on the array length.”,您将看到我根据显示剥离文本节点的位置以及是否object 已定义。 我正在检查长度而不是 undefined,因为 typeof == undefined 由于某种原因根本不起作用。 显然,事情仍在溜走。
知道为什么我仍然在数组中得到未定义的对象吗?
我为这么大的帖子道歉!
提前致谢
我已经修改了您的eoTextSearch()
function 以删除对全局变量的依赖以换取闭包:
$.fn.extend({
// helper function
// recurses into a DOM object and calls a custom function for every descendant
eachDescendant: function (callback) {
for (var i=0, j=this.length; i<j; i++) {
callback.call(this[i]);
$.fn.eachDescendant.call(this[i].childNodes, callback);
}
return this;
},
// your text search function, revised
eoTextSearch: function () {
var text = document.createTextNode("test").textContent
? "textContent" : "innerText";
// the "matches" function uses an out param instead of a return value
var matches = function (pat, outArray) {
var isRe = typeof pat.test == "function";
return function() {
if (this.nodeType != 3) return; // ...text nodes only
if (isRe && pat.test(this[text]) || this[text].indexOf(pat) > -1) {
outArray.push(this.parentNode);
}
}
};
// this is the function that will *actually* become eoTextSearch()
return function (stringOrPattern) {
var result = $(); // start with an empty jQuery object
this.eachDescendant( matches(stringOrPattern, result) );
return result;
}
}() // <- instant calling is important here
});
然后你可以做这样的事情:
$("body").eoTextSearch("foo").filter(function () {
return $(this).closest("tr").is(":visible");
});
从搜索结果中删除不需要的元素。 不需要“重新计算数组长度” 。 或者你直接使用each()
并决定做什么。
我无法完全理解您的代码,但最可能的问题是您正在从数组中删除项目,但之后没有缩小数组。 简单地删除项目将返回“未定义”,并且不会折叠数组。
我建议您执行以下操作之一:
将数组复制到新数组,但只复制那些未定义的项
仅使用那些未定义的数组项。
我希望这能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.