40 const filterData
41 = optionsData.filter((item) => {
42 let str = item.label;
43 return -1 !==str.indexOf(event.target.value); 44 });
45 this.setState({ filterdOptionData: filterData });
46 };
Yoda conditions are named so because the literal value of the condition comes first while the variable comes second.
For instance,
if ("red" === color) {
// ...
}
Yoda condition is fixed by switching the literal and variable.
This is called a Yoda condition because it reads as, "if red equals the color", similar to the way the Star Wars character Yoda speaks. Compare to the other way of arranging the operands:
if (color === "red") {
// ...
}
This typically reads, "if the color equals red", which is arguably a more natural way to describe the comparison.
if ("red" === color) {
// ...
}
if (true == flag) {
// ...
}
if (5 > count) {
// ...
}
if (-1 < str.indexOf(substr)) {
// ...
}
if (color === "red") {
// ...
}
if (flag === true) {
// ...
}
if (count < 5) {
// ...
}
if (str.indexOf(substr) > -1) {
// ...
}