[英]How can I use a ternary statement to simplify this code?
In my first if statement, it checks to see if a word from the array food
and window[items]
appears in the text. 在我的第一个if语句中,它会检查数组
food
和window[items]
中的单词是否出现在文本中。 In my second if statement, it checks to see if a word from the array window[items]
appears in the text 在我的第二个if语句中,它检查文本中是否出现数组
window[items]
中的单词
Note that which if statement is run depends on whether or not the array authors
is empty - the first statement will run if it isn't empty and the second will run if it is empty. 请注意,运行if语句取决于数组
authors
是否为空 - 如果第一个语句不为空,则第一个语句将运行,如果第二个语句为空,则第二个语句将运行。
if(food.length > 0 && food.some(text => tag.textContent.includes(text)) && window[items].some(text => tag.textContent.includes(text))) {
ele[i].style.display = "block";
}else{
ele[i].style.display = "none";
}
if(food.length < 1 && window[items].some(text => tag.textContent.includes(text))) {
ele[i].style.display = "block";
}else{
ele[i].style.display = "none";
}
I have tried simplifying this code by using a ternary operator, but it returns this error: 我尝试使用三元运算符来简化此代码,但它返回此错误:
Uncaught SyntaxError: Unexpected token )
未捕获的SyntaxError:意外的令牌)
if((food.length > 0 ? food.some(text => tag.textContent.includes(text))) && window[items].some(text => tag.textContent.includes(text))) {
ele[i].style.display = "block";
}else{
ele[i].style.display = "none";
}
You can achieve it by accumulation of two statements and checking with an OR (||) condition. 您可以通过累积两个语句并使用OR(||)条件进行检查来实现它。 Something like
就像是
ele[i].style.display = (food.length > 0 && food.some(text => tag.textContent.includes(text)) && window[items].some(text =>
tag.textContent.includes(text)) || (food.length < 1 &&
window[items].some(text => tag.textContent.includes(text))) ? "block" :"none"
But that looks terrible to a new code reader. 但对于新的代码阅读器而言,这看起来很糟糕。 For the sake of juniors in the team, stick with the current way please ?
为了团队中的青少年,请坚持目前的方式吗?
So, the condition is: if ( food
is empty or the tag text contains any of food
) and the tag text contains any of the window[items]
: 因此,条件是:if(
food
是空的或标签文本包含任何food
) 并且标签文本包含任何window[items]
:
ele[i].style.display = (!food.length || food.some(text => tag.textContent.includes(text)))
&& window[items].some(text => tag.textContent.includes(text)) ? "block" : "none";
To have a ternary operator you need ?
要有三元运算符
?
and :
. 并且
:
。 Pseudocode: 伪代码:
if (condition) { block1 } else { block2 } => condition ? block1 : block2
Condition: food.length > 0
条件:
food.length > 0
Block 1: food.some(text => tag.textContent.includes(text)) && window[items].some(text => tag.textContent.includes(text))
第1块:
food.some(text => tag.textContent.includes(text)) && window[items].some(text => tag.textContent.includes(text))
Block 2: window[items].some(text => tag.textContent.includes(text))
第2块:
window[items].some(text => tag.textContent.includes(text))
So this should work: 所以这应该工作:
if (food.length > 0 ? food.some(text => tag.textContent.includes(text)) && window[items].some(text => tag.textContent.includes(text)) : window[items].some(text => tag.textContent.includes(text)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.