使用数组的对象的目标属性 - Javascript

时间:2012-10-26 16:58:52

标签: javascript arrays object properties target

我遇到这个问题。

//Targeting a property of an object using an array

myArray = ["property0","property1","property2","property3"];

myObject = {};

myObject[myArray[0]] = "value0";
//accepts this line as myObject.property0 = "value0";  

alert(myObject.myArray[0]);  // <-- how can I target this using my Array?
//this fails

// alert(myObject.property0); 
//this works

1 个答案:

答案 0 :(得分:1)

与设置方式相同:

alert(myObject[myArray[0]])