www.webdeveloper.com
Results 1 to 4 of 4

Thread: Referencing arrays of objects

  1. #1
    Join Date
    Apr 2006
    Posts
    8

    Referencing arrays of objects

    I am trying to return a JSON string from a server with a list of locations. I want to populate a div with checkboxes and the names of the locations.

    This is the JSON string I am currently working with:
    Code:
    [{"loccode":2,"locdesc":"Atlanta"},
    {"loccode":1,"locdesc":"Charlotte"},
    {"loccode":0,"locdesc":"NA"},
    {"loccode":3,"locdesc":"Test1A"}]
    I have a similarly formed data definition that is hard coded in the javascript for permissions that looks like this:
    Code:
    	var prm = [
    		{"id":110,"idtext":"110","prmdesc":"Users"},
    		{"id":130,"idtext":"130","prmdesc":"Forms"},
    		{"id":140,"idtext":"140","prmdesc":"Documents"},
    		{"id":150,"idtext":"150","prmdesc":"View Files"},
    		{"id":160,"idtext":"160","prmdesc":"Upload Files"},
    		{"id":240,"idtext":"240","prmdesc":"Update General"}]
    In the case of the prm variable, I am able to reference items using an index such as prm[1].prmdesc. But if I use the eval function to store the JSON string in a variable, I am not able to use that access method. What's more, I haven't been able to find any programmatic way to cycle through the entries.

    If I store this to oLoc, I can type:
    Code:
    alert(oLoc["1"].locdesc)
    That will return "Charlotte". But if I setup a for .. next loop:
    Code:
    for (x=0; x < oLoc.length; x++)
    {
        alert(oLoc[x].locdesc);
    }
    It creates an error that it is undefined. If I add another variable s and assign it to be x stored as a string, it is still undefined.

    I have also tried for in and this returns only the last entry:
    Code:
    for (var o in oLoc)
    {
        alert(oLoc[o].locdesc);
    }
    When i run that bit of code it displays Test1A and that is all.

    I am certain I am missing something basic, but I have not been able to figure out what it is. Can anyone lend a hand?

    Thanks

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Hi,

    I'm having a little trouble understanding your problem. You mention a variable "oLoc," but you never provided code that explained how it was defined. Furthermore, I don't understand the following paragraph:


    In the case of the prm variable, I am able to reference items using an index such as prm[1].prmdesc. But if I use the eval function to store the JSON string in a variable, I am not able to use that access method. What's more, I haven't been able to find any programmatic way to cycle through the entries.
    If "oLoc" is the first snippet of code you posted, you can cycle through it several ways. Following is a simple for loop that alerts the locdesc property of each item.

    Code:
    var oLoc = [{"loccode":2,"locdesc":"Atlanta"},
    {"loccode":1,"locdesc":"Charlotte"},
    {"loccode":0,"locdesc":"NA"},
    {"loccode":3,"locdesc":"Test1A"}];
    
     for(var i=0; i<oLoc.length; i++){
       alert ( oLoc[i].locdesc );
     }
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  3. #3
    Join Date
    Apr 2006
    Posts
    8
    Thanks for the reply Jona.

    The code for the oLoc variable is:
    Code:
    oLoc = eval("(" + sJSON + ")")
    sJSON is the code from the first snippet.

    The code in your example is what I started with, but that produced an undefined error. I have gotten it to work by placing the code to cycle through the array in its own function and I pass the array to it. It works fine like that, but I can not get it to work without separating the code into its own function. It just seems strange to me. I have double checked that no variables are being reused inside the loop.

  4. #4
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Hi,

    Can you post the code? I'm having trouble visualizing exactly what your program looks like at this point.

    You might try running some tests against
    oLoc to see what JavaScript is doing with it. Alert the variable, and then alert the typeof oLoc and see if it returns "string," "object," etc. Since arrays are objects in JavaScript, you should be getting the result "object." If you're getting string, that's where your problem is.
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles