Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9204191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:39:05+00:00 2026-06-17T23:39:05+00:00

I orginally was going to ask a question on why it wasn’t working. But

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:39:06+00:00Added an answer on June 17, 2026 at 11:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I originally started this question in another thread, but that thread was sorta, kinda
I'm currently working on a website that works without any javascript. I'm going to
Before going into the details of this question, I'd like to point out that
I'm working on a small utility class that originally was going to read in
I was originally going to make this a longer question, but I feel like
The solution to the question orginally worked but as of January 26. 2013 it
This was originally going to be a question about how to implement this because
I'm going to try asking this again, but better than the last time. I
I really didn't know how to ask this question in a simple fashion. I
I have a process that's going to initially generate 3-4 million PDF files, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.