Kei*_*out 0 javascript arrays ajax jquery object
我试图访问存储在数组中的每个对象的text属性。该数组是对象内部另一个属性(结果)的值。
我像这样使用jQuery从服务器检索对象。
$.ajax({
url: "https://api.parse.com/1/classes/chats",
cache: false,
type: 'get',
async: false,
success: function(data){
console.log(data);
}
});
Run Code Online (Sandbox Code Playgroud)
最后的日志语句是查看我收到的信息。当然,这是我需要做的事情,但是我似乎无法破解代码。因此,我有一个带有result属性和Array值的对象。数组是对象的数组,每个对象都有自己的属性。我只是对如何获得所需的东西感到困惑。也许是朝着正确方向轻轻推一下?
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.
Run Code Online (Sandbox Code Playgroud)
你要
data.results[0].text
Run Code Online (Sandbox Code Playgroud)
[] 将让您获得数组的单个元素
. 将让您获得任何对象的属性。
您可能需要循环:
for (var i = 0; i < data.results.length; ++i) {
console.log(data.results[i].text);
}
Run Code Online (Sandbox Code Playgroud)