最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How can I access properties inside objects inside other objects in javascript - Stack Overflow

programmeradmin2浏览0评论

I am trying to access the text property of each object stored in an array. The array is a value of another property, results, inside an object.

I retrieve the object from a server using jQuery, like this.

       $.ajax({
       url: "",
       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. 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.

I am trying to access the text property of each object stored in an array. The array is a value of another property, results, inside an object.

I retrieve the object from a server using jQuery, like this.

       $.ajax({
       url: "https://api.parse./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. 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.
Share Improve this question edited Oct 15, 2013 at 20:57 Keith Grout asked Oct 15, 2013 at 19:40 Keith GroutKeith Grout 9191 gold badge11 silver badges32 bronze badges 1
  • I'd remend using a tool like Firebug for Firefox which will allow you to set breakpoints and examine objects. It's great for learning as well as debugging – geedubb Commented Oct 15, 2013 at 19:44
Add a ment  | 

3 Answers 3

Reset to default 5

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

发布评论

评论列表(0)

  1. 暂无评论