0

In PHP it is common to use variable variables, e.g.

foreach(['key1', 'key2', 'key3'] as $key) {
    if (!$object->$key)
        $object->$key = 'add some values';
}

i wonder how can one do that in javascript?

Practical use case:

we have multiple required input fields (e.g.: name, surname, email) in form with no-validate, values are automatically pushed to this instance (meaning we can access values with this.name, this.surname, etc). We need to ensure these fields are filled.

One could write something like:

if (!this.name)
    this.errors['name'] = 'This field is required.';

if (!this.surname)
    this.errors['surname'] = 'This field is required.';

if (!this.email)
    this.errors['email'] = 'This field is required.';

I am trying to come up with something more compact and accurate like:

['name', 'surname', 'email'].forEach(function (field) {
    if (!this.[field])
        this.errors[field] = 'This field is required.';
});
dorkyboi
  • 3
  • 3
  • Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – VLAZ Sep 18 '19 at 14:45
  • What is `this` in your JS example? i.e. is it an array, an object, or something else? – Martin Sep 18 '19 at 14:45
  • 3
    `this.[field]` -> `this[field]` although you also need [to make sure the correct context is preserved](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – VLAZ Sep 18 '19 at 14:45
  • 1
    Also, those aren't variable variables - in PHP a variable variable is `$key = "dynamic"; $$key = "dynamically set";` where `$$key` would resolve to the variable `$dynamic` – VLAZ Sep 18 '19 at 14:48
  • @Taplar [variable variables are a thing in PHP](https://www.php.net/manual/en/language.variables.variable.php). Look at my other comment. Basically, you use the value of one variable to access another by name. It's...very unwieldy. – VLAZ Sep 18 '19 at 14:49
  • Very strange. Just from a javascript stand point I can see how someone might call `myVar` used as `someObject[myVar]` as a "variable variable", but to me that's just weird phrasing. @VLAZ – Taplar Sep 18 '19 at 14:52
  • @Taplar well, as I said - the PHP code OP shows doesn't even use variable variables. It uses normal variables to access properties dynamically, same as `obj[variable]` in JS. So, yes - the title is confusing and inaccurate. – VLAZ Sep 18 '19 at 14:54
  • @VLAZ thanks, that did it. I keep forgetting in javascript one can access object's properties not only with dot but also with brackets. Sorry for messed title, i meant `variable property`. Thats how its called right? – dorkyboi Sep 18 '19 at 15:00

1 Answers1

1

U can do that almost same way as in php:

class Person {

  constructor(name, surname, email) {
    this.name = name;
    this.surname = surname;
    this.email = email;
    this.errors = {}
  }
  
  validate () {
    ['name', 'surname', 'email'].forEach((field) => {
      if (!this[field])
          this.errors[field] = field + ' is required.';
    });
  }
  
  getErrors() {
    return this.errors;
  }
}

let s = new Person('john','doe');
s.validate();
console.log(s.getErrors());
Mischa
  • 1,591
  • 9
  • 14