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: "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. 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.
1
  • I'd recommend 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 Commented Oct 15, 2013 at 19:44

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

To help clarify for me, my thought is that 'results' is a property with one value, an array with 10 objects. If it only has one value (and that value has 10 properties- each with some corresponding values), then how is it possible to use bracket notation on 'results'( ie, why doesn't result[1] return undefined, since there's only 1 item, an array). Your code works I'm just trying to understand it.
To expand, in my brain it should be data.results.array[i].text. Why is this wrong?
@keith The first message you see in the log is saying that what you're logging (data) contains one property, called results, which is an array with ten elements in it. Once you're dealing with the array, of course, indexing to ten should make more sense. It may make more sense if you play more with your console, e.g. type data.results, and data.results[3].
Thanks Scott, for the answer and for the feedback. Of course, 'results' is the actual array. I don't know why I wasn't getting that. The problem was a lack of understanding the relationship between properties and values I suppose, because now looking back on it, the question (and answer) is actually very simple. Cheers!
2

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]);
    }
}

1 Comment

jslint would complain about not having hasOwnProperty check ;)
1

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

3 Comments

that's assuming the use of jquery however
indeed - that's a fair assumption :)
John, from the info in the question data appears to contain just one thing, a property called results that is an array of objects. In any case you should say this.text, because $(this).text would get a reference to jQuery's .text() method...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.