Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript How do you loop through an array within an object?

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35694
    schotty
    Member

    Hi there,

    I’m trying to loop through this here:

    var data = {"test":"yes","people":[{"name":"Chris","age":27,"msg":"Hello"},{"name":"John","age":76,"msg":"Some Text"}],"computers":6,"remember":true}

    I Know how to output “Test: yes” and even how to output “Name: Chris” using

    alert("Name: " + data.people[0].name);

    But every time I try and loop through “data.people.name” or whatever code I use, it never seems to work?

    Basically I want it to output this as HTML like so:


    Chris

    John

    Obviously there will be more than 2, so I need the code to produce the divs too.

    How would I do that either with jQuery or Javascript? I know it’s probably very simple, but it’s confusing me so much…

    Thanks for the help.

    #92957
    schotty
    Member

    @Senff Thank you! that worked great, I was VERY close with my tries, but yours worked perfectly.

    #92963
    schotty
    Member

    Hmmm, sorry to bother you again, but how do you append that to a div? for example a div with the ID of “response”? I can’t seem to get it working?

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.