R. Matveev R. Matveev - 1 year ago 76
Javascript Question

Get Object property value calling it with string

I pass some object to my function. I have some array of predefined variables. I need so set specific attribute in

(which name is in the array) to the value of property of passed object. How can I access 'em?

function a(MyObject) {
var list = ["Auto", "Cook", "West", "Vodka"];

for (var i = 0; i < list.length; i++) {


I tried 2 ways, none of 'em works in js:

  • var a = MyObject[list[i]];

  • var a = MyObject.list[i];

I understand that there's wrong syntax, but I have no idea how can I do this.

Answer Source

It is as easy as doing

for(var i=0; i<list.length; i++){
youObject[list[i]] = somevalue;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download