سؤال

There is a javascript expression that assigns name variable to renamed variable. It is always the same with one exclusion:

renamed =  name == 'John' ? 'Johnny' : name;

However, I want two exclusions:

  1. rename John to Johny
  2. rename Alex to Alexander
  3. All other names are assigned with no changes.

Is it possible to write this expression in one string?

renamed =  (name == 'John' || name == 'Alex') ? <____> : name;

I need it to be done in one string.

Thank you.

هل كانت مفيدة؟

المحلول

(name === 'John' && 'Johny') || (name === 'Alex' && 'Alexander') || name;
  • If name is John, then it goes to the next part in the && expression and returns Johny.

  • If name is Alex, then the like in the first case returns Alexander.

  • If neither of them is true, then return the name as it is.

Demo

This solution works because, in JavaScript, && operator evaluates the expression in the left and if it is falsy then the value will be returned and the right hand side expression will not be evaluated at all.

If the expression in the left evaluates to be Truthy, then the expression on the right side will be evaluated and the result will be returned as it is. For example

console.log(1 && 2);
# 2
console.log(0 && 2);
# 0

It first evaluates 1, it is Truthy so it 2 is evaluated and the value is returned. That is why it prints 2.

In the second case, 0 is evaluated to be Falsy. So, it is returned immediately. That is why it prints 0.

The same way

console.log("John" && "Johny");
# Johny

John will be evaluated to be Truthy and so Johny will also be evaluated and returned. That is why we get Johny.

As per ECMA 5.1 Standard, Truthiness of an object will be decided, as per the following table

+-----------------------------------------------------------------------+
| Argument Type | Result                                                |
|:--------------|------------------------------------------------------:|
| Undefined     | false                                                 |
|---------------|-------------------------------------------------------|
| Null          | false                                                 |
|---------------|-------------------------------------------------------|
| Boolean       | The result equals the input argument (no conversion). |
|---------------|-------------------------------------------------------|
| Number        | The result is false if the argument is +0, −0, or NaN;|
|               | otherwise the result is true.                         |
|---------------|-------------------------------------------------------|
| String        | The result is false if the argument is the empty      |
|               | String (its length is zero); otherwise the result is  |
|               | true.                                                 |
|---------------|-------------------------------------------------------|
| Object        | true                                                  |
+-----------------------------------------------------------------------+

Note: The last line, Object, which includes both objects and Arrays.

نصائح أخرى

renamed = (name == 'john') ? 'johney': (name == 'alex'? 'alexander' : name);

you can try this:

renamed = (name == 'John' ? 'Johnny' : (name == 'Alex' ? 'Alexander' : name));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top