Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I orginally was going to ask a question on why it wasn't working. But as I commented the code I realized that I could access the text array style and item was giving me the index. It took me a while to find this as the solution, however I would have much rather used item.text in the for loop. Is this [my posted answer] the valid way to loop through JSON objects in Javascript?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

There's no such thing as a JSON object - once you've parsed your JSON (which is a string) you end up with simply an object. Or an array.

Anyway, to loop through an array of objects, use a traditional for loop. A for..in loop may work, but is not recommended because it may loop through non-numeric properties (if somebody has been messing with the built-in array).

In your specific case, if obj.body.items is an array then do this:

for (var i = 0; i < obj.body.items.length; i++) {
    // obj.body.items[i] is current item
    console.log(obj.body.items[i].text);
}

You can also, arguably, make the code a bit neater by keeping a reference directly to the array rather than accessing it via the whole obj.body. chain every time:

var items = obj.body.items;
for (var i = 0; i < items.length; i++) {
    console.log(items[i].text);
}

"I would have much rather used item.text in the for loop"

In your answer - which really should've been posted as part of the question - because you are using a for..in loop your item variable is being set to each index in turn as a string, e.g., on the first iteration it will be the string "0", and strings don't have a text property so item.text doesn't work - although it should give you undefined, not null. But you can do this:

var item;
for (var i = 0; i < obj.body.items.length; i++) {
    item = obj.body.items[i] // get current item
    console.log(item.text);  // use current item
}

That is, declare a variable called item that you set to reference the current array item at the beginning of each loop iteration.

share|improve this answer
    
My answer is an answer because it shows my process and the answer that I used, which does work. This is the code I am now using: for (var item in obj.body.items) {console.log(obj.body.items[item].text);} –  michaellindahl Jan 28 '13 at 6:58
    
I would've posted just that code in the question and said "This works to access the properties of objects in an array, but is there a better way to do it? I'd like to be able to somehow say item.text in the loop." Your question as it now stands is kind of vague about what you're trying to do even taken with the code in your answer, and hypothetically if you were to accept somebody else's answer that would push your own answer down in the list such that other people reading the question would have to go searching to figure out what you were talking about. –  nnnnnn Jan 28 '13 at 7:18
    
Sorry, this is the first time I used the Q&A style thing and didn't realize it would just post my answer as an answer below, I thought it was going to do something fancier and keep them both together. Lesson learned. –  michaellindahl Jan 29 '13 at 6:14
add comment
    <script type="text/javascript">
    ...   
    obj = JSON.parse(xmlhttp.responseText);
    // displays the object as expected
    console.log(obj);
    // display the string next as expected
    console.log(obj.body.next);
    // displays the text of the item as expected
    console.log(obj.body.items[0].text);
    for (var item in obj.body.items) {
    // displays the index value of the item
        console.log(item);
    // displays null - WHAT?? HUH?
        console.log(item.text);
    // displays the text - finally
        console.log(obj.body.items[item].text);
    }
    <script>
share|improve this answer
    
What?­­­­­­­­­­ –  Derek 朕會功夫 Jan 28 '13 at 5:53
    
I answered my own question, therefore I checked the checkbox for Q&A style... –  michaellindahl Jan 28 '13 at 6:52
    
The question is Is this the valid way to... but you never answer your question in this answer. –  Derek 朕會功夫 Jan 29 '13 at 2:47
    
Yea, this is the first time I used the Q&A style thing and didn't realize it would just post my answer as an answer below, I thought it was going to do something fancier. –  michaellindahl Jan 29 '13 at 6:12
    
Now you know it wouldn't be any fancier. :) –  Derek 朕會功夫 Jan 29 '13 at 6:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.