在javascript中,如何获取存储在对象属性中的数组中的值,其父对象存储在数组中?

时间:2014-04-02 15:35:47

标签: javascript arrays

在javascript中,如何获取存储在对象属性中的数组中的值,其父对象存储在数组中?

这是我的目标。我可以看到它里面有什么,但我不知道如何在数组中访问这些值。我从不同的问题尝试了很多方法,但我没有找到方法......必须有一些我做得不对的事。这是代码:

    this.Object3dName = New3DobjectName;
    this.Object3dId = NumberOf3Dobjects;
    this.Object3dFaces = new Array();
    var face1 = new Array(); //Faces properties in index order: texture1(background in css3),texture2(background-image in css3), type3(possible types are triangle, rectangle and circle), faceHeight4, faceWidth5, X-position6, Y-position7, Z-position8, X-axis9, Y-axis10, Z-axis11, SelectedOrNot12 and reflect13
    face1[0] = "default";       // texture1
    face1[1] = "default";       // texture2
    face1[2] = "default";       // texture3
    face1[3] = "rectangle";     //type  
    face1[4] = "100px";         //height  
    face1[5] = "100px";         //width
    face1[6] = 0;               // X-postion
    face1[7] = 0;               // Y-postion
    face1[8] = 0;               // Z-postion
    face1[9] = 0;               // X-axis
    face1[10] = 0;              // Y-axis
    face1[11] = 0;              // Z-axis 
    face1[12] = "NotSelected";  // SelectedOrNot12
    this.Object3dFaces.unshift(face1);

    all3dobjectArray.push(this);

    console.log("You clicked on the object who's id is: " + myobjectindexpostion);
    console.log("You clicked on the object : " + all3dobjectArray[myobjectindexpostion]);
    n = all3dobjectArray[myobjectindexpostion].Object3dFaces;
    m = n[6];
    console.log( "n: " + n + "deg  m: " + m );

1 个答案:

答案 0 :(得分:1)

此代码可以正常工作:

this.Object3dFaces = new Array();
var face1 = new Array();
face1[0] = "default";       // texture1
face1[1] = "default";       // texture2
face1[2] = "default";       // texture3
face1[3] = "rectangle";     //type  
face1[4] = "100px";         //height  
face1[5] = "100px";         //width
face1[6] = 0;               // X-postion
face1[7] = 0;               // Y-postion
face1[8] = 0;               // Z-postion
face1[9] = 0;               // X-axis
this.Object3dFaces.push(face1);
all3dobjectArray = new Array();
all3dobjectArray.push(this);

var shouldBeZero = all3dobjectArray[0].Object3dFaces[0][6];
console.log('shouldBeZero', shouldBeZero);

这是你的意图吗?

如果您将face1推到Object3dFaces,并且它是唯一的元素,那么您将在索引6处获得undefined。您想要访问{{1}在索引0处,然后在此之后访问face1的第六个元素。见上文。