简体   繁体   English

可以缩短此代码吗?

[英]Can this code be shortened?

This is an equation to check whether 1 of the 3 elements has value. 这是一个公式,用于检查3个元素中的1个是否具有值。 If yes, all elements must have a value: 如果是,则所有元素必须具有一个值:

if(   (a!='' || b!='' || c!='')   &&   (a=='' || b=='' || c=='')   )
    alert('Please fill all elements');

You could use the modulo operator, making this a solution for 4, 5, or more variables as well -- just adapt the final number: 您可以使用模运算符,也使它成为4、5或更多变量的解决方案-只需修改最终数字即可:

if ((!a+!b+!c)%3) 

The ! ! (a boolean negation) makes !a a boolean result, based on the fact that an empty string is falsy, while non-empty strings are truthy, so you get true or false in these cases respectively. (布尔否定)使!a为布尔结果,这是基于以下事实:空字符串是虚假的,而非空字符串是真实的,因此在这两种情况下,您分别得到truefalse

Booleans are coerced to numbers (0 or 1) when added up. 加起来时,布尔值被强制为数字(0或1)。 So the sum is either 0, 1, 2, or 3. 所以总和是0、1、2或3。

The % operator gives the remainder after division by 3, which for the possible values is the original value except for 3: in that case the result is 0. So we get a non-zero value for, and only for, the cases where we want to show a validation error. %运算符给出除以3的余数,对于3的可能值,它是原始值, 3 除外 ;在这种情况下,结果为0。因此,对于且仅对于以下情况,我们得到非零值想要显示验证错误。

In the context of an if condition this value is coerced to a boolean value, and non-zero numerical values are truthy. if条件的情况下,此值被强制为布尔值,并且非零数值是真实的。

Dynamic number of inputs 动态输入数

If you have an array of inputs, then you can use a similar pattern with filter : 如果您有输入数组,那么可以将类似的模式与filter

// Assume arr = [a,b,c];
if ( arr.filter(Boolean).length%arr.length )

Note how the Boolean function is used to essentially do the same as the double negation did (actually, it would be the same if I had used !! instead of ! earlier on -- now it gives the opposite boolean value, but that works just as well). 请注意, Boolean函数在本质上与双重否定法是一样的(实际上,如果我早先使用!!而不是! ,它将是相同的-现在它提供了相反的布尔值,但这仅适用于以及)。

Afterthoughts 事后思考

Although this answers your question, you should really ask yourself if it is worth it to reduce code that much: readable code is a much more important asset than shorter code, certainly when execution time is not influenced by it. 尽管这回答了您的问题,但您应该真正问自己:减少这么多代码是否值得:可读代码比短代码更为重要,当然,这不受执行时间的影响。

This may not seem shorter for 3 variables, but it definitely is for a greater number: 对于3个变量,这似乎并不短,但对于更大的变量,它肯定是更短的:

 let a = "test", b = "", c = ""; let arr = [a, b, c] let filteredArr = arr.filter(item => item === '') if (filteredArr.length > 0 && filteredArr.length < arr.length) { alert('Please fill all elements'); } 

You can do 你可以做

if([a,b,c].find(entry => entry == '').length && [a,b,c].find(entry => entry !== '').length) {
     alert('Please fill all elements');
}

IMO is more readable as well, it first checks if any is empty, if so it check if any has data, and if both is true, asks to fill all elements IMO也更具可读性,它首先检查是否有空,如果有,则检查是否有数据,如果两者都为真,则要求填充所有元素

 if(!( !!a === !!b && !!b === !!c))

That should do it. 那应该做。 Basically this fails if all three conditions are equally truthy or falsy. 基本上,如果所有三个条件都是真实的或虚假的,则此操作将失败。

So, some elements are empty, but not all elements are empty? 因此,某些元素为空,但并非所有元素都为空?

const emptyCount = (a=='') + (b=='') + (c=='');
if (emptyCount > 0 && emptyCount != 3) {
    alert('Please fill all elements');
}

Why don't you just do like 你为什么不喜欢

if (a === "" || b === "" || c === ""){
  alert('Please fill all elements');
}

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

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