filter(x => !!x)与filter(x => !!x && x)相同吗?

huangapple go评论93阅读模式
英文:

Is filter(x => !!x) the same as filter(x => !!x && x)?

问题

filter(x => !!x)是否等同于filter(x => !!x && x)?在这两种情况下,该方法返回相同的结果,但我想了解答案的原理。

英文:

Is filter(x => !!x) the same as filter(x => !!x && x)? In both cases the method returns the same thing but I want to know the theories of the answer.

答案1

得分: 1

I think it's the same, in the filter method, the return value will be regarded as Boolean.

!!x === Boolean(!!x) === Boolean(x)

!!x && x === Boolean(!!x && x) === Boolean(x) && Boolean(x) === Boolean(x)

Hahaha, so it's equivalent, that's what I think.

But in an assignment expression in the following code:

const a = !!x && x
const b = !!x

The results of these two expressions are different.

For && operator, exp1 && exp2, if exp1 returns false, the exp2 will not execute, but when exp1 returns true, the exp2 will still execute.

So in assignment expressions:

  • !!x will always return true || false, so to speak Boolean.
  • !!x is false:
    • !!x will always equal !!x && x.
  • !!x is true:
    • !!x equals !!x && x when x is a Boolean.
    • In other cases, the two are not equivalent.
英文:

I think it's the same ,in the filter method, the return value will be regarded as Boolean

!!x === Boolean(!!x) === Boolean(x)

!!x && x === Boolean(!!x && x) === Boolean(x) && Boolean(x) === Boolean(x)

Hahaha, so it's equivalent, that's what I think


But in a assignment expression in the following code

const a = !!x && x
const b = !!x

The results of these two expressions are different

For && operator, exp1 && exp2, if exp1 returns false, the exp2 will not execute, but when exp1 returns true, the exp2 will still execute.

So in assignment expressions

  • !!x will always return true || false,so to speak Boolean
  • !!x is false
    • !!x will always equals !!x && x
  • !!x is true
    • !!x equals !!x && x when x is a Boolean
    • In other cases, the two are not equivalent

答案2

得分: 0

让我们运行一些测试,看看这些不同的值对每个值的输出有什么不同。这将展示每个值的情况

const values = [-1, 0, 1, 2, {a:'foo'}, null, undefined, true, false, 'true', 'false', 'foo'];

let htmlStr = '';
values.forEach(x => {
  htmlStr += `
  <tr>
    <td>${JSON.stringify(x, null, 0)}</td>
    <td><code>${JSON.stringify(!!x, null, 0)}</code></td>
    <td><code>${JSON.stringify((!!x && x), null, 0)}</code></td>
  </tr>`;
});

document.querySelector('tbody').innerHTML = htmlStr;

filter函数的目的是确定什么应该包括在结果中,因此一切都应该返回一个boolean值。这里的结果显示,!!x方法总是返回一个布尔值,所以对于绝对基本的情况,这个方法效果最好。

然而,请记住,仅仅因为它是一个布尔值,并不一定意味着它转换成布尔值的内容在你的应用程序中是有意义的! 例如:你确定希望字符串"false"被转换为true吗?

总结一下:!!x会每次都将事物转换为布尔值,但你需要确保被转换的内容对你的用例是有意义的。这并不是一个通用解决方案,因为有很多方法可能会出错。

英文:

Let's run some tests and see what different values output for each of those. This will show you what happens for each value

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const values = [-1, 0, 1, 2, {a:&#39;foo&#39;}, null, undefined, true, false, &#39;true&#39;, &#39;false&#39;, &#39;foo&#39;];

let htmlStr = &#39;&#39;;
values.forEach(x =&gt; {
  htmlStr += `
  &lt;tr&gt;
    &lt;td&gt;${JSON.stringify(x, null, 0)}&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;${JSON.stringify(!!x, null, 0)}&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;${JSON.stringify((!!x &amp;&amp; x), null, 0)}&lt;/code&gt;&lt;/td&gt;
  &lt;/tr&gt;`;
});

document.querySelector(&#39;tbody&#39;).innerHTML = htmlStr;

<!-- language: lang-css -->

table, th, td {border:1px solid #DDD; border-collapse:collapse;}
td, th {padding: .25rem;}
code{color: #b71313;}

<!-- language: lang-html -->

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Value&lt;/th&gt;
      &lt;th&gt;&lt;code&gt;!!x&lt;/code&gt;&lt;/th&gt;
      &lt;th&gt;&lt;code&gt;!!x &amp;&amp; x&lt;/code&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;&lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

The purpose of a filter function is to determine what should or should not be included in the results, thus everything should return a boolean. The results here show that the !!x method always returns a boolean, so for the absolute basics this works best.

However, keep in mind that just because it's a boolean doesn't necessarily mean that what it's converting into a boolean makes sense in your application! For example: Are you sure you want the string &quot;false&quot; to be converted into true?

To summarize: !!x will convert things to booleans every time, but you need to make sure what is being converted makes sense for your use case. It's not a catch-all since there are so many ways this could go wrong.

答案3

得分: -1

!!x 不等于 !!x && x


但是为什么在过滤器中使用时返回相同的结果?

首先要了解在条件语句中如何评估变量,请参考:

在许多用例中,为了仅过滤真值,可以简单地将过滤方法编写为:

filter(x => x)

而假值过滤可以编写为:

filter(x => !x)

现在,考虑一些示例变量

const x = "A"

const notX     = !x        // false  (!"A")
const notNotX  = !!x       // true   (!!"A")/(!notX)

const whatIsThis = (notNotX && x) // "A" || 可以理解为 (true && "A")

// 在任何其他上下文中,它都会被评估为 x 的原始值,在这种情况下为 "A"
console.log(whatIsThis) 
// 在布尔上下文中,它被视为真值
if(whatIsThis) console.log(true)

console.log(whatIsThis === notNotX)  // false,whatIsThis 不等于 notNotX

每当 x 本身被评估为 真值 时,!!x 将返回 true

另一种理解为什么过滤器返回相同结果的方式是尝试将语句解读为:

  • !!xtrue
  • !!x && xtrue && "truthy"
英文:

!!x is not equal to !!x &amp;&amp; x


But Why does it return the same thing when used in filter?

first to understand how variables are evaluated during a conditional statement, refer:

for a lot of use case, in order to filter only truthy value, filter method can simply be written as follow:
filter(x =&gt; x)

while falsy filter can be written as:
filter(x =&gt; !x)

now, given some sample variable

const x = &quot;A&quot;

const notX     = !x        // false  (!&quot;A&quot;)
const notNotX  = !!x       // true   (!!&quot;A&quot;)/(!notX)

const whatIsThis = (notNotX &amp;&amp; x) // &quot;A&quot; || can be read as (true &amp;&amp; &quot;A&quot;)

// in any other context, it is evaluated as original value of x, in this case: &quot;A&quot;
console.log(whatIsThis) 
// in the context of boolean, it is evaluated as truthy 
if(whatIsThis) console.log(true)

console.log(whatIsThis === notNotX)  // false, whatIsThis is not equal to notNotX

anytime that x itself would be evaluated as truthy,
!!x will returns true

another other way to understand why filter return same result, try reading the statement as

  • !!x is true
  • !!x &amp;&amp; x is true &amp;&amp; &quot;truthy&quot;

huangapple
  • 本文由 发表于 2023年7月10日 21:58:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定