SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    589
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loop associatuve array

    Hello Forums,

    How do I loop through an associative array

    Code Java:
    var myArray = [{'a': 'value1'}, {'b': 'value2'}, {'c':'value3'}];
     
    for (var i = 0; i < myArray.length; i++){
       document.write(myArray[i]);
    }

    This is giving me [object Object][object Object][object Object]

    I want the result to be :
    a -value 1
    b-value 2
    c-value 3

    Thanks
    Don't Panic

  2. #2
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    589
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NVM
    the structure of my json is wrong

    Code Java:
    var myArray = {'a': 'value1', 'b': 'value2', 'c':'value1'};
     
    for (var key in myArray) {
      if (myArray.hasOwnProperty(key)) {
     
        document.write(key + " -> " + myArray[key]) 
     
      }
    }

    Cheers
    Don't Panic


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •