R. Matveev R. Matveev - 4 months ago 9
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

SomeObject
(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++) {
SomeObject.getSomeAttribute(list[i]).setValue(MyObject.list[i]);
}

return;
}


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.

RAF RAF
Answer

It is as easy as doing

for(var i=0; i<list.length; i++){
youObject[list[i]] = somevalue;
}
Comments