简体   繁体   English

从对象Javascript访问数据

[英]Accessing data from Object Javascript

I have created this object : 我创建了这个对象:

    var hemicycle = {

    Groupe : "Group1" [{
        Member : [{
            Name : "MemberName",
            Siege : "SiegeNumber",
            Vignette : "PhotoURL"
        }]

    }]


};

I try to display some data from it but I can't access any. 我尝试显示其中的一些数据,但无法访问任何数据。 When I type hemicycle in the dev tools of Chrome I get this : 当我在Chrome的开发工具中输入hemicycle时,我得到以下信息:

在此处输入图片说明

I also tried to display "Group1" from the object but it won't let me, I typed this : 我也尝试从对象显示“ Group1”,但是它不允许我输入:

在此处输入图片说明

I don't know why my object is not defined ? 我不知道为什么我的对象没有定义?

Any help is appreciated. 任何帮助表示赞赏。

Well, the issue is, that you actually don't have a valid object. 好吧,问题是,您实际上没有有效的对象。 Groupe has really weird structure, what is it supposed to be? Groupe结构确实很怪异,应该是什么? Array? 阵列? Then try this: 然后试试这个:

var hemicycle = {
  Groupe: [{
    Member : [{
      Name : "MemberName",
      Siege : "SiegeNumber",
      Vignette : "PhotoURL"
    }]

  }]
};

Your problem is that your object itself is invalid. 您的问题是对象本身无效。 Two ways to improve that. 有两种改善方法。 Choose whatever fits your needs best. 选择最适合您的需求。

var hemicycle = {

Groupe : "Group1",
Content : [{
    Member : [{
        Name : "MemberName",
        Siege : "SiegeNumber",
        Vignette : "PhotoURL"
    }]

}]

}; };

var hemicycle = {
    Groupe : [{
        Member : [{
           Name : "MemberName",
           Siege : "SiegeNumber",
           Vignette : "PhotoURL"
    }]

}]

}; };

this simple example should solve your problem 这个简单的例子应该可以解决您的问题

var myObj = {
    "1": { address: "44 buxton gardens berea durban", lat: "25.686", lng: "58.1156"},
    "2": { address: "44 buxton gardens berea durban", lat: "0.0086", lng: "-0.00056"}
 }

  alert(myObj['1']['address']); //this get the address value of the first element

//using foreach statement to access the element and key

 for (key in myObj){
  alert(key); //this will display the 1
 //use can say
 alert(myObj[key]['address']); // this gets the value of address
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM