Array.filter()始终返回相同的输出
问题描述:
以下代码总是返回相同的输出。Array.filter()始终返回相同的输出
var array = ['v1', 'v2', 'v3', 'v4'];
console.log(array.filter(doThis));
function doThis(item) {
var element;
var variable;
if (item === 'thisString' || item === 'thatString') {
return false;
} else {
element = document.getElementById(item);
if (element.children > 0) {
variable = value;
}
if (element.children.length < 2) {
if (element.children.length === 0) {
return true;
} else {
if (valueA && variable > 15) {
element.removeChild(element.children[0]);
document.getElementById('container-a').appendChild(element.children[0]);
} else if (valueB && variable < 16) {
element.removeChild(element.children[0]);
document.getElementById('container-b').appendChild(element.children[0]);
}
return true;
}
} else {
if (valueA && variable > 15) {
return false;
} else if (valueB && variable < 16) {
return false;
} else {
return true;
}
}
}
}
下面是实际的代码按要求...
var x = targetPointsArray.filter(testTargetPointsValidity);
console.log(x);
function testTargetPointsValidity(item) {
var pointElement;
var childCheckerValue;
if (item === 'point-00-RA' || item === 'point-RA-00') {
return false; // unless racking
} else {
pointElement = document.getElementById(item);
if (pointElement.children > 0) {
childCheckerValue = Number(pointElement.children[0].id.substring(8,10));
}
if (pointElement.children.length < 2) {
if (pointElement.children.length === 0) {
return true;
//validatePoint(pointElement);
} else {
if (p1.isActing && childCheckerValue > 15) {
pointElement.removeChild(pointElement.children[0]);
document.getElementById('point-BA-25').appendChild(pointElement.children[0]);
} else if (p2.isActing && childCheckerValue < 16) {
pointElement.removeChild(pointElement.children[0]);
document.getElementById('point-25-BA').appendChild(pointElement.children[0]);
}
return true;
//validatePoint(pointElement);
}
} else {
if (p1.isActing && childCheckerValue > 15) {
return false;
} else if (p2.isActing && childCheckerValue < 16) {
return false;
} else {
return true;
//validatePoint(pointElement);
}
}
}
}
为什么下面的代码总是返回相同的输出(['v1', 'v2', 'v3', 'v4']
),即使该函数的计算结果为return false
?我在这里误解了什么?
我试过使用不同的返回值无济于事。我试过注释掉return false
(即// return false;
)。我一直在寻找各种其他的*问题,其中没有一个似乎解决了这个问题。
正如ChrisG指出的,问题是我简单地忘了一些代码:element.children > 0
当然应该是element.children.length > 0
。
答
由于ChrisG指出,问题是我简直忘了一些代码... element.children > 0
当然应该是element.children.length > 0
。
答
过滤函数创建一个基于传递给它的阵列中的其他阵列,它不改变它,所以你应该尝试:
console.log(array);
var newArray = array.filter(doThis);
console.log(array); // => still the same
console.log(newArray); // => filtered
并覆盖,使用:array = array.filter(doThis);
例子:
var array = [1,3,46,-8,45,45,45,19,15,23,194,555];
array = array.filter(function(a){return a>40});
console.log(array);
array.filter的内部函数对每个返回的元素返回true,对于每个被跳过的元素返回false。所以我猜测,从v1到v4,函数返回true。如果你想[真,真,真,真],你应该使用array.map。 – James
以下是该文档的第一句话:“filter()方法创建_a new array_,其中包含所有通过所提供函数实现的测试的元素。”另外,我们无法测试您的代码,因为您的问题中缺少HTML部分。 –
@apsillers对不起,我也试过'console.log(array.filter(doThis))',当我知道它应该是不同的它不是。 – Anthony