[英]How can I access properties inside objects inside other objects in javascript
I am trying to access the text property of each object stored in an array. 我试图访问存储在数组中的每个对象的text属性。 The array is a value of another property, results, inside an object.
该数组是对象内部另一个属性(结果)的值。
I retrieve the object from a server using jQuery, like this. 我像这样使用jQuery从服务器检索对象。
$.ajax({
url: "https://api.parse.com/1/classes/chats",
cache: false,
type: 'get',
async: false,
success: function(data){
console.log(data);
}
});
The log statement at the end is to see what I am receiving. 最后的日志语句是查看我收到的信息。 Naturally this is where I need to be doing something, but I can't seem to crack the code.
当然,这是我需要做的事情,但是我似乎无法破解代码。 So, I have an object with the result property and Array value.
因此,我有一个带有result属性和Array值的对象。 The array is an array of objects, each with their own properties.
数组是对象的数组,每个对象都有自己的属性。 I'm just a bit confused on how to get what I need.
我只是对如何获得所需的东西感到困惑。 Perhaps a gentle nudge in the right direction?
也许是朝着正确方向轻轻推一下?
Object {results: Array[10]} //object returned
results: Array[10] //value is an array of objects
0: Object // object '0' expanded...
createdAt: "2013-10-15T19:13:43.576Z"<br><br>
objectId: "uzGerloXA7"
text: "RoboChat: I'm sorry Dave, I can't allow you to do that." // I need this!
updatedAt: "2013-10-15T19:13:43.576Z"
username: "RoboChat"
1:Object // and I need it for each of these objects.
2:Object
3:Object
etc...
9:Object //this is the last object.
You want 你要
data.results[0].text
[]
will let you get an individual element of an array []
将让您获得数组的单个元素
.
will let you get properties of any object. 将让您获得任何对象的属性。
You'll probably want a loop: 您可能需要循环:
for (var i = 0; i < data.results.length; ++i) {
console.log(data.results[i].text);
}
Just specify the array index followed by the property name: 只需指定数组索引,后跟属性名称即可:
data.results[0].propName;
To iterate, you could do: 要进行迭代,您可以执行以下操作:
//Iterate the array of objects
for (var i = 0; i < data.results.length; i++) {
//Iterate over the keys of a specified object
for (var key in data.results[i]) {
if (data.results[i].hasOwnProperty(key))
console.log(data.results[i][key]);
}
}
you could do some iteration like : 您可以进行一些迭代,例如:
var allText = [];
$.each(data.results,function(i,obj){
allText.push(obj.text);
});
and all texts are stored in allText ah and its jquery moe 并且所有文本都存储在allText ah及其jquery moe中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.