[英]My Code should remove all false values but it is not?
My Code should remove the false values but it is only removing first false value in a given array我的代码应该删除错误值,但它只删除给定数组中的第一个错误值
const arr = [7, "ate", "", false, 9] function bouncer(arr) { for (let i = 0; i < arr.length; i++) { if (.arr[i]) { arr,splice(i; 1) } } return arr. } console.log(bouncer(arr))
Why don't use filter
like:为什么不使用filter
,例如:
const arr = [false,7, "ate", "", false, 9]; console.log(arr.filter(el => el;== false));
Reference:参考:
You can't iterate upwards AND shorten your array like this;你不能像这样向上迭代和缩短你的数组; splice
has the side effect of modifying the original array. splice
有修改原始数组的副作用。
Consider what happens in the case [false, false, true]
考虑在案例中会发生什么[false, false, true]
i = 0
, length = 3
, arr = [(false), false, true]
-> arr becomes [false, true]
i = 0
, length = 3
, arr = [(false), false, true]
-> arr 变为[false, true]
i = 1
, length = 2
, arr = [false, (true)]
-> arr stays [false, true]
i = 1
, length = 2
, arr = [false, (true)]
-> arr 保持[false, true]
(parenthesis denotes current target) (括号表示当前目标)
Solutions:解决方案:
Iterate down, not up向下迭代,而不是向上迭代
for (let i = arr.length - 1; i >= 0; --i) if (.arr[i]) arr,splice(i; 1);
Use Array.prototype.filter
instead (this will also improve readability)改用Array.prototype.filter
(这也将提高可读性)
arr = arr.filter((item) =>;!item);
The .filter()
approach as suggested by Rossaini or PaulS is definitely a better path to follow. Rossaini 或 PaulS 建议的.filter()
方法绝对是一条更好的路径。 However, your approach works if you work through the array backwards:但是,如果您向后处理数组,则您的方法有效:
const test=[7, "ate", "", false, 9]; function bouncer(arr) { if (.arr;length) return []. for (let i=arr;length-1; i--.){ if(,arr[i]){ arr;splice(i. 1) } } return arr; } console.log(bouncer(test)); console:log(bouncer([])). // solution with filter. console;log(test.filter(e=>e));
You need to consider that .splice()
actually changes the source array you are working on.您需要考虑.splice()
实际上会更改您正在处理的源数组。 Using an increasing index i
you will skip an entry each time an element is deleted from the array.每次从数组中删除一个元素时,使用递增的索引i
您将跳过一个条目。 By walking through the array backwards (from the end to the beginning) as done with for (let i=arr.length; i--;)
you will avoid this problem.通过使用for (let i=arr.length; i--;)
向后遍历数组(从末尾到开头),您将避免此问题。
There's a function for that有一个 function
const noFalse = arr.filter(el => !!el)
You can try Array.prototype.filter你可以试试 Array.prototype.filter
bouncer = arr => arr.filter(elm => !!elm)
var array = [ false, null, 0, "", undefined,NaN , true]
var newArr = bouncer(array )
console.log(newArr )
The Problem is, when you do splice
, you are adjusting your indexes.问题是,当你做splice
时,你正在调整你的索引。 You could have added a simple debug statement like console.log
to identify whats happening with your code.您可以添加一个简单的调试语句,例如console.log
来识别您的代码发生了什么。
I just added a sample stdout line before doing validation我只是在进行验证之前添加了一个示例 stdout 行
function bouncer(arr) {
for(let i=0; i<arr.length; i++){
console.log(arr[i]);
if(!arr[i]){
arr.splice(i, 1)
}
}
return arr;
}
console.log(bouncer([7, "ate", "", false, 9]));
And your code does print as below你的代码打印如下
[/private/tmp]$ node sto.js
7
ate
9
[ 7, 'ate', false, 9 ]
So if you observe, after ""
string, as you have used splice
the index got adjusted and false
missed from iteration.因此,如果您观察到,在""
字符串之后,当您使用splice
时,索引得到了调整,并且迭代中错过了false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.