【发布时间】:2021-07-07 20:09:14
【问题描述】:
我在一个数组中有一堆对象,我想知道是否可以通过使用数组索引来调用该对象中的某个变量?
例如。
function myObject(x,y) {
this.x = x;
this.y = y;
}
var myArray[];
for (i = 0 ; i<10 ; i++) {
myArray.push(new myObject(10, 15));
}
//Now i want to fetch the x value of the fourth object in this array
var x4;
x4 = myArray[3.x];
此代码不起作用,但我想知道是否有类似的东西可以工作?
【问题讨论】:
-
我是
myArray[3].x -
您可以使用
myArray[3].x
标签: javascript arrays list