英文:
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 returntrue || false
, so to speakBoolean
.!!x
is false:!!x
will always equal!!x && x
.
!!x
is true:!!x
equals!!x && x
whenx
is aBoolean
.- 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 returntrue || false
,so to speakBoolean
!!x
is false!!x
will always equals!!x && x
!!x
is true!!x
equals!!x && x
whenx
is aBoolean
- 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:'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;
<!-- language: lang-css -->
table, th, td {border:1px solid #DDD; border-collapse:collapse;}
td, th {padding: .25rem;}
code{color: #b71313;}
<!-- language: lang-html -->
<table>
<thead>
<tr>
<th>Value</th>
<th><code>!!x</code></th>
<th><code>!!x && x</code></th>
</tr>
</thead>
<tbody></tbody>
</table>
<!-- 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 "false"
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
另一种理解为什么过滤器返回相同结果的方式是尝试将语句解读为:
!!x
是true
!!x && x
是true && "truthy"
英文:
!!x
is not equal to !!x && 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 => x)
while falsy filter can be written as:
filter(x => !x)
now, given some sample variable
const x = "A"
const notX = !x // false (!"A")
const notNotX = !!x // true (!!"A")/(!notX)
const whatIsThis = (notNotX && x) // "A" || can be read as (true && "A")
// in any other context, it is evaluated as original value of x, in this case: "A"
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
istrue
!!x && x
istrue && "truthy"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论