Using the value of conditions in if / else

I am wondering if it is possible to access the value of the condition directly, as in the following example.

var a = ["pear", "kiwi", "orange", "apple"] if(a.indexOf("orange") !== -1){ console.log(this) //as a.indexOf("orange") has been evaluated already above this prints 2 } 

It will also make triple operators less bloated.

 var a = ["pear", "kiwi", "orange", "apple"] var b = ((a.indexOf("orange") !== -1) ? this : '') //"this" equals 2 

thanks

EDIT: Eliminating this issue for future visitors. Basically this question is about getting the obtained value of what is evaluated in the if / else statement. In the example

 var a = ["pear", "kiwi", "orange", "apple"] if(a.indexOf("orange") !== -1){ //is basically if(2 !== -1) console.log(this) //would then be "2" from the already evaluted a.indexOf from above } 
+5
source share
7 answers

You can simply save it in front of the operator if the goal is to not evaluate twice. The answer to your personal question is no.

 const orangeIndex = a.indexOf("orange") if (orangeIndex !== -1) { console.log(orangeIndex) } 

The same concept applies to the ternary operator.

As others have shown, you can also declare a variable and do the actual assignment in the if statement itself, but IMO makes your code less readable without adding any value.

+6
source

There is no implicit object, but you can assign a comparison value to a variable:

 var a = ["pear", "kiwi", "orange", "apple"], result; if (result = (a.indexOf("orange") !== -1)){ console.log(result); } 

edit - the same method can be applied if you just want part of the evaluated expression:

 var a = ["pear", "kiwi", "orange", "apple"], result; if ((result = a.indexOf("orange")) !== -1){ console.log(result); } 

Now the return value .indexOf() stored in result instead of the comparison result.

+4
source

If you need really complex code, you can assign a variable in this state.

 var orangeIndex; var a = ["pear", "kiwi", "orange", "apple"]; if ((orangeIndex = a.indexOf("orange")) !== -1) { console.log(orangeIndex); } 

You can also do this in triple form:

 var orangeIndex; var a = ["pear", "kiwi", "orange", "apple"] var b = (((orangeIndex = a.indexOf("orange")) !== -1) ? orangeIndex : ''); console.log(b); 

In both cases, do not forget the brackets around the destination. This is necessary because the assignment has a lower priority than the comparison operators, so otherwise the value of the variable would be true or false .

+2
source

I will reorganize your code. You can save the result of the condition in a variable.

 var a = ["pear", "kiwi", "orange", "apple"]; var isOrange = a.indexOf("orange") !== -1; if(isOrange){ console.log(this) //as a.indexOf("orange") has been evaluated already above this prints 2 } var a = ["pear", "kiwi", "orange", "apple"] var b = isOrange ? this : '') //"this" equals 2 
0
source

Yes, it is actually very simple. Example

 if (yourvariable = yourcondition) { //Do something } else if (yourvariable2 = yourcondition2) { //Do something else } else if ((yourvariable3 = yourcondition3) || true) { //This is an else, but you memorized a condition inside it //Do something } 

When you assign a value to a variable, then the variable will hold onto that value, and the result of the statement will be the value, so

 if (foo = bar) { //Some operations } 

logically equivalent

 if (bar) { foo = bar; //Some operations } 

but it is longer, and if bar turns out to be a function with a large number of operations, then this is not ideal for evaluating it twice.

0
source

It's as close to

 var a = ["pear", "kiwi", "orange", "apple"], result; if ((result = a.indexOf("orange")) !== -1) { console.log(result); } 

Here @result = index, not the result of a logical operation suggested by Pointy.

0
source

You can accomplish this through memoization. lodash provides a method for this called memoize .

Creates a function that remembers the result of func. If a resolver is provided, it defines a cache key for storing the result based on the arguments provided for the memoized function. By default, the first argument provided to the memoized function is used as the map cache key. The func function is called with this binding of the memoized function.

 // your collection const fruit = ["pear", "kiwi", "orange", "apple"] /* * a basic indexOf function that we can memoize. * @example * indexOf(fruit)('kiwi') // 1 */ const indexOf = list => Array.prototype.indexOf.bind(list) /* * a memoized version of IndexOf that is seeded with the list * @example * memoizedIndexOfA('kiwi') // 1 */ const memoizedIndexOfA = _.memoize(indexOf(fruit)) // the first time it is called in the `if` it is calculated if (memoizedIndexOfA("orange") !== -1){ // the value was previously calculated, so retrieve from the cache. console.log(memoizedIndexOfA("orange")) } 
0
source

Source: https://habr.com/ru/post/1262095/


All Articles