0

I have this object:

let user = {
    name: undefined,
    age: undefined,
    city: undefined,
}

and these arrays

props = ["name", "age", "city"];
values = ["alan", 90, "london"];

and i want to do something like this

for (i=0; i<props.length; i++){
    user.${prop[i]} = values[i];
}

But I can't figure out how to use template strings as I pretend

The idea is to generate a function that can fill given properties of an object with given values, avoiding writing a lot of function calls

  • 1
    format like `user[ props[i] ] = values[i];` should be what you need. – Gavin May 22 '20 at 11:49
  • 4
    There's no reason to use a template string for it, see the linked question's answers. Just `user[prop[i]] = values[i];` does what you want. **If** the property name were being created from multiple things, you might use a template string, for instance ```user[`${prop[i]}_foo`] = value[i];```, but not for what you're doing above. – T.J. Crowder May 22 '20 at 11:49

0 Answers0