简体   繁体   English

在条件表达式中不必要地使用 Boolean 文字

[英]Unnecessary use of Boolean literals in conditional expression

I have a function that checks whether a value is found in array.我有一个 function 检查是否在数组中找到一个值。 I want to return a true or false.我想返回一个 true 或 false。 Current code works but throws and js-standerd/es-lint error "Unnecessary use of boolean literals in conditional expression" I've searched through a ton of these error messages here but can't seem to wrap my head around it.当前代码有效,但抛出 js-standerd/es-lint 错误“在条件表达式中不必要地使用 boolean 文字”我在这里搜索了大量这些错误消息,但似乎无法理解它。 To me this says 'If the value is found return true otherwise false'对我来说,这表示“如果找到该值,则返回 true,否则返回 false”

let found = value.find(val => {
  return val === item
})
return found ? true : false

I tried this我试过这个

  return value.find(val => {
     return val === item
   }) || false

Which works but doesn't return a Boolean if found, it returns item.哪个有效但不返回 Boolean 如果找到,它返回项目。

I know i can make this work in multiple ways but i'm just trying to figure out whether my code is bad or incorrect or whether es-lint is flagging it sort of incorrectly.我知道我可以通过多种方式完成这项工作,但我只是想弄清楚我的代码是否错误或不正确,或者 es-lint 是否错误地标记了它。

The linter is complaining about this: linter 抱怨这个:

return found ? true : false

It says, if found is truthy return true otherwise return false .它说,如果found是真的返回true否则返回false This structure is referred to as a 'ternary' operator and has been in use since the early days of C, if not before.这种结构被称为“三元”运算符,从 C 的早期开始就一直在使用,如果不是更早的话。 The ? ? operator evalautes the condition on the left and returns the first argument if the condition has evaluated to true, else it returns false.运算符评估左边的条件,如果条件评估为真,则返回第一个参数,否则返回假。

The problem with your code is that returning the condition itself is the equivalent of returning the boolean literals true or false .您的代码的问题在于,返回条件本身等同于返回布尔文字truefalse Therefore, the check and the literals are unnecessary and can be removed.因此,检查和文字是不必要的,可以删除。 Though, because this is javascript you might want to double negate the condition before returning it, to force it to be a boolean.但是,因为这是 javascript,您可能希望在返回之前双重否定条件,以强制它成为布尔值。 So, the result looks like this:所以,结果是这样的:

return !!found

This is easier to read and there is less chance of it being implemented wrong or misunderstood in the future.这更易于阅读,并且将来被错误实施或误解的可能性更小。

Of course, this could be taken further:当然,这还可以进一步:

return !!value.find(val => {
  return val === item
})

In this way, you don't need to even introduce the symbol found into the code at all.这样,您甚至根本不需要在代码中引入found的符号。 Also, this would be better with some() , but I think your question is more about the ternary operator than how to search a list.此外,使用some()会更好,但我认为您的问题更多是关于三元运算符而不是如何搜索列表。

My issue was an unnecessary 'else if'.我的问题是一个不必要的'else if'。

This produced an error:这产生了一个错误:

// val: "A" | "B" | "C"
const val = "B";

if (val === "A") {
     // do something
} else if (val === "B" || val === "C") {
     // do something else
}

Removing the 'else' fixed the error:删除“其他”修复了错误:

// val: "A" | "B" | "C"
const val = "B";

if (val === "A") {
     // do something
}

if (val === "B" || val === "C") {
     // do something else
}

I suppose the reasoning is readability.我想原因是可读性。

return value.some(val => { return val === item; });

暂无
暂无

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

相关问题 错误:在条件表达式中不必要地使用布尔文字 - Error: Unnecessary use of boolean literals in conditional expression Eslint:在条件表达式中不必要使用布尔文字 - Eslint: Unnecessary use of boolean literals in conditional expression Javascript ESLint错误-条件表达式中不必要使用布尔文字 - Javascript ESLint Error - Unnecessary use of boolean literals in conditional expression 错误在条件表达式中不必要地使用布尔文字 no-unneeded-ternary - error Unnecessary use of boolean literals in conditional expression no-unneeded-ternary Javascript:错误在条件表达式 no-unneeded-ternary 中不必要地使用 boolean 文字 - Javascript: error Unnecessary use of boolean literals in conditional expression no-unneeded-ternary 为何使用!! 将变量强制转换为布尔值以用于条件表达式? - Why use !! to coerce a variable to boolean for use in a conditional expression? FlowJS:如何使用联合类型和布尔文字 - FlowJS: How to use union types and boolean literals 在 hasura GQL 突变中使用具有条件逻辑的模板文字 - Use template literals with conditional logic in hasura GQL mutation 如何在条件中更改表达式的求值以检查Javascript中的布尔值? - How do I change the evaluation of the expression in the conditional to check for a boolean in Javascript? 不使用关系运算符的 JavaScript 条件表达式 - JavaScript Conditional expression without the use of Relational operator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM