Is it possible to condition an operator += in a ternary?

Asked

Viewed 154 times

4

I have an empty object:

obj = {}

I’m trying to check if there’s a key op1 in that object. If there is I want to add the key and any value, getting like this:

obj = { op1: 10 } // O valor 10 é hipotético.

Case the key op1 exists in the object obj i want to add any value to the value already existing in this key. For example, if:

obj = { op1: 10 }

And I come with a value 15, would be:

obj = { op1: 25 }

So far so good. I have a code that does this check and sum using if...else:

var valor = 15;
if(obj["op1"]){
   obj["op1"] += valor;
}else{
   obj["op1"] = valor;
}

If the key op1 already exist in the obj, i just update the value, if it doesn’t exist, I create the key op1 with the value of the variable valor.

My question is: is there any way to eliminate this if...else using a ternary operator, since the only difference between the if and the else is the operator +=? Or else I’d have some way to simplify that if...else?

  • 1

    Great question, I went through exactly this situation a few days ago and ended up creating a code with if...else as you quote.

2 answers

8


Such operators as the +=, do two things:

  • perform an arithmetic operation (in this case the addition operation), and:
  • Assign the result of the back opration to variable.

To learn more, see documentation.

Thus, the following two examples are equivalent, the second being considered a type of abbreviation of the first:

variable = variable + 10;
variable += 10;

Therefore, you cannot do any type of check to the right of the += to find out if he is undefined or not, since the value on the right will simply be the value to be used in the arithmetic operation.

You can use ternary operators to do this in only one expression, but the logic will be the same as if/else. Behold:

const obj = { keyA: 5 };
const value = 10;

const keyA = 'keyA';
obj[keyA] ? obj[keyA] += value : obj[keyA] = value;

const keyB = 'keyB';
obj[keyB] ? obj[keyB] += value : obj[keyB] = value;

console.log(obj);

But in that case I don’t think it’s worth it, unless you need it to be just a single expression... I see only usefulness of you using the short form of Arrow functions, since the if/else ends up being much less complicated. :)

But you can choose not to use operators like +=. In that case, you can do something like this:

const obj = { keyA: 5 };
const value = 10;

const keyA = 'keyA';
obj[keyA] = (obj[keyA] || 0) + value;

const keyB = 'keyB';
obj[keyB] = (obj[keyB] || 0) + value;

console.log(obj);

If I’m gonna do it in one expression, I personally would opt for the latter option.

In short, these are just a few more examples of the fact that there are infinite ways to achieve the same goal in programming. :)

  • 2

    I agree, his last suggestion is more readable than making assignment in the core of the ternary conditional - by the way, I comment on the readability of this operator in cases like this here.

  • 1

    @bfavaretto, in his reply, the "flow control" part explains exactly why I [personally] would not use a ternary in the first example. Although I’m not wrong, I don’t see much point in that case (unless a single expression is needed - which is rarely the case). :]

  • 1

    I looked at the first suggestion I scared, I found someone had stolen your account. I only reassured myself after reading the last option.

  • 2

    The last code was very elegant and simple.

-5

In fact, just put the obj["op1"] += valor; if it does not exist, it has no value and will assume the value only of the variable.

  • 1

    I took this test and it generated a undefinedTeste with string and NaN with numbers... :(

  • 2

    The result of this will be NaN, since you’re trying to add undefined with valor, which is a number.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.