[英]How does this nested ternary operator work using javascript?
I am trying to understand what does this nested ternary operator mean using javascript?我想了解这个嵌套的三元运算符使用 javascript 是什么意思?
below is the code,下面是代码,
const columns = !isUser1Readable
? allColumns.filter(column => !columnIdsUser1.includes(column.id))
: !isUser2Readable
? allColumns.filter(column => !columnIdsUser2.includes(column.id))
: allColumns;
what i have understood?我明白了什么?
seems like if isUser1Readable evaluates to false it filters allcolumns array such that it doesn't contain column from columnIdsUser1似乎如果 isUser1Readable 的计算结果为 false,它会过滤 allcolumns 数组,使其不包含来自 columnIdsUser1 的列
if isUser1Readable is true and isUser2Readable is false then filters allcolumns array such that it doesn't contain column from columnIdsUser2如果 isUser1Readable 为真且 isUser2Readable 为假,则过滤所有列数组,使其不包含来自 columnIdsUser2 的列
But when does allColumns is assigned to columns?但是什么时候将 allColumns 分配给列呢?
Could someone help me understand what this nested ternary operator does above.有人可以帮我理解这个嵌套的三元运算符在上面做了什么。 Thanks.
谢谢。
but when does allColumns is assigned to columns?
但是什么时候将 allColumns 分配给列?
In the remaining case, ie where both isUser1Readable
and isUser2Readable
are true.在剩下的情况下,即
isUser1Readable
和isUser2Readable
都为真。
The chained ternary expression can be interpreted as an if... else if... else
sequence:链接的三元表达式可以解释为
if... else if... else
序列:
let columns;
if (!isUser1Readable) {
columns = allColumns.filter(column => !columnIdsUser1.includes(column.id));
} else if (!isUser2Readable) {
columns = allColumns.filter(column => !columnIdsUser2.includes(column.id));
} else {
columns = allColumns;
}
The conditional operator¹ is greedy .条件运算符¹是贪婪的。 The expression is clearer with indentation:
缩进后表达式更清晰:
const columns = !isUser1Readable
? allColumns.filter(column => !columnIdsUser1.includes(column.id))
: !isUser2Readable
? allColumns.filter(column => !columnIdsUser2.includes(column.id))
: allColumns;
!isUser1Readable
is true
, columns
is assigned the result of allColumns.filter(column =>.columnIdsUser1.includes(column.id))
.!isUser1Readable
为true
,则为columns
分配allColumns.filter(column =>.columnIdsUser1.includes(column.id))
的结果。!isUser2Readable
is true
, columns
is assigned the reuslt of of allColumns.filter(column =>.columnIdsUser2.includes(column.id))
!isUser2Readable
为true
,则为columns
分配allColumns.filter(column =>.columnIdsUser2.includes(column.id))
的结果columns
is assigned allColumns
.columns
被分配allColumns
。but when does allColumns is assigned to columns?
但是什么时候将 allColumns 分配给列?
When both !isUser2Readable
and !isUser2Readable
are false
.当
!isUser2Readable
和!isUser2Readable
都为false
时。
¹ Although people frequently call it "the ternary operator," it's actually just a ternary operator (an operator accepting three operands). ¹ 尽管人们经常称其为“三元运算符”,但它实际上只是一个三元运算符(接受三个操作数的运算符)。 It's currently JavaScript's only ternary operator, but that could change.
它是目前 JavaScript 唯一的三元运算符,但这可能会改变。 It's name is "the conditional operator."
它的名字是“条件运算符”。
As the ternary operator is define like this因为三元运算符是这样定义的
condition ? exprIfTrue : exprIfFalse
Condition
is evaluate to true Condition
评估为真时处理的代码Condition
is evaludate to false Condition
评估为假的情况下处理的代码The code which you provide can be refactor like this您提供的代码可以像这样重构
let allColumns = [];
if(!isUser1Readable) {
allColumns = allColumns.filter(column => {
return !columnIdsUser1.includes(column.id);
});
} else if (!isUser2Readable) {
allColumns = allColumns.filter(column => {
return !columnIdsUser2.includes(column.id);
});
}else {
columns = allColumns;
}
You can learn more about Conditional (ternary) operator您可以了解有关条件(三元)运算符的更多信息
I quite like using nested conditional operators in expressions but I find that authoring style does matter to improve readability.我非常喜欢在表达式中使用嵌套的条件运算符,但我发现编写风格确实对提高可读性很重要。 (Although YMMV.)
(尽管 YMMV。)
This is how I would have written it:这就是我的写法:
const columns =
( !isUser1Readable ? allColumns.filter(column => !columnIdsUser1.includes(column.id))
: !isUser2Readable ? allColumns.filter(column => !columnIdsUser2.includes(column.id))
: allColumns);
Which reads as:内容如下:
const columns =
( if !X : do A
: else if !Y : do B
: otherwise do C)
So allColumns
is returned if and only if both booleans ( X & Y ) are true
meaning that the first two checks will both evaluate to false
.因此,当且仅当两个布尔值 ( X & Y ) 都为
true
时,才会返回allColumns
,这意味着前两个检查的计算结果都为false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.