简体   繁体   English

Array.prototype.filter 自动过滤掉falsey值

[英]Array.prototype.filter automatically filtering out falsey value

Does the filter function automatically filter out falsey values such as false, null, or undefined? filter 函数是否会自动过滤掉 false、null 或 undefined 等虚假值? If so, how to stop it from doing that.如果是这样,如何阻止它这样做。

I need to filter out all 0 in an array, so I used array.filter callback, and I realized that the filter function removes falsey values, even when I want to keep them.我需要过滤掉数组中的所有 0,所以我使用了 array.filter 回调,我意识到过滤器函数会删除假值,即使我想保留它们。 Is there a way around this?有没有解决的办法?

EDIT: This is the code that I have, and doing a deep comparison didn't return the falsey values.编辑:这是我拥有的代码,并且进行深度比较并没有返回 falsey 值。

function removeZero(arr) {
  return arr.filter(function(element){
    if(element!==0){
      return element;
    }
  });
}

EDIT 2: Here's the repl.it link that contains a test array content, and when I ran it the result filters out false, null, and undefined.编辑 2:这是包含测试数组内容的 repl.it 链接,当我运行它时,结果会过滤掉 false、null 和 undefined。 https://repl.it/BAiK https://repl.it/BAiK

Use the !== operator使用!==运算符

[1,2,3,0,[],{},false,undefined].filter(function(e){return e !== 0})
// Output: [1, 2, 3, Array[0], Object, false, undefined]

compare with the output of the != operator (which you don't want to use)!=运算符的输出进行比较(您不想使用)

[1,2,3,0,[],{},false,undefined].filter(function(e){return e != 0})
// Output: [1, 2, 3, Object, undefined]

This is the code you provided https://repl.it/BAiK这是您提供的代码https://repl.it/BAiK

var removeZeros = function (arr) {
  return arr.filter(function(element){
    if(element!==0){
      return element; // here is the error, you need to return a boolean. (true to add element to final array, false to ignore element)
    }
  });
}

console.log(removeZeros([false, 1, 2, 3, null, 0, 4, undefined, 5, 0]));
// Output: [ 1, 2, 3, 4, 5 ]

You see, the return value for the filter function needs to be a boolean value, not the element you want to return.你看,过滤器函数的返回值需要是一个布尔值,而不是你想要返回的元素。 When you return false, the element is not added to the final array, when you return true, the element being processed is returned.当您返回 false 时,该元素不会添加到最终数组中,当您返回 true 时,将返回正在处理的元素。 See my edit with your code fixed查看我的编辑并修复了您的代码

This is your code fixed这是你的代码固定

var removeZeros = function (arr) {
  return arr.filter(function(element){
    return element !== 0 // this returns a boolean
  });
}

console.log(removeZeros([false, 1, 2, 3, null, 0, 4, undefined, 5, 0]));
// Output: [ false, 1, 2, 3, null, 4, undefined, 5 ]

 var result = [1, 2, 3, 0, false, undefined, null, -1].filter( function (e) { return e !== 0; }); document.getElementById('out').innerHTML = result.reduce(function (r, a) { return r + '\\n ' + a + ' [' + (typeof a) + ']'; }, '[') + '\\n]';
 <pre id="out"></pre>

your example does not return true, but the value itselft, which is evaluated.您的示例不返回 true,而是返回值本身,这是被评估的。 Array.filter() include the element if the return value is truthy.如果返回值为真,则 Array.filter Array.filter()包含元素。

function removeZero(arr) {
    return arr.filter(function(element) {
        if (element !== 0){
            return element; // <-- falsy values are not true.
        }
    });
}

just只是

[1,2,3,0,[],{},false,undefined].filter(Boolean)  // return [1, 2, 3, [], {}]

here Boolean is a function, which when all the falsy value come in, it will return false这里Boolean是一个函数,当所有的falsy value进来时,它会返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM