SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to loop through Javascript Struct Values?

    I have a struct like this:
    Code JavaScript:
    function Sampe(name, value, unit)
    {
       this.name = name;
       this.value = value;
       this.unit = unit;
    }

    I then have an array of instances of this struct. I'm trying to create a search feature and need to loop through each instance of the struct in my array. Then I need to loop through each value in the struct and check it for a substring. If a substring is found the entire struct instance is printed and the search will skip to the next struct instance. Right now my loops are not working properly. The loop below with 'item' is not correct as 'item' just prints out a number (should be a text value). Any suggestions?

    Code JavaScript:
    function searchView(text)
    {
       document.getElementById("searchResult").innerHTML = "";
       if(text.length > 2){
          //Search through all conditions
          for(data in conditions) {
             //Search through each field in the condition struct
             for(item in data){
                //Check to see if 'text' is a substring of the condition field
                if(item.lastIndexOf(text)){
                   var string = "";
                   //print the entire condition
                   for(item in data){
                      string += data[item] + " | ";
                   }
                   document.getElementById("searchResults").innerHTML = string;
                   //Skip to next condition
                   break;
                }
             }
          }
       }
    }
    Follow Me On Twitter: BryceRay

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    A problem I can see is that you're creating global variables all the time. Also, you have two identical for...in statements that use the same variables names, so they're going to get overwritten. I mean the for (item in data) statements.

    To stop them being globals, you need to use var in the for...in statements:

    Code javascript:
    for (var data in conditions) {
      // do things with data or conditions[data]
    }

  3. #3
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok here is an updated version of the function which I hope is more syntactically correct. I believe the loop through the array of conditions is working fine. The trouble i'm still having is then looping through the values of each 'condition'. I'm not sure on the syntax for that as it seems every combination I've tried has either printed out numbers, NaN, or nothing.

    Code JavaScript:
        if(text.length > 2){
            //Search through all conditions
            string = "Conditions:<br />";
            for(var data in conditions) {
                //Search through each field in each condition struct instance
                for(var item in data){
                    //Check to see if 'text' is a substring of the condition field
                    if(item.lastIndexOf(text)){
                        var string = "Conditions: <br />";
                        //if found a match print all the items in the active condition then skip to next condition
                        for(var itemRepeat in data){
                            string += itemRepeat = + " | ";
                        }
                        document.getElementById("searchResults").innerHTML = string;
                        break;
                    }
                }
            }
       }
    }
    Follow Me On Twitter: BryceRay

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You seem to not understand the difference between global and local variables. At the top of that script you're defining "string". Later on you do "var string". Either the first one should be "var string" and the second shouldn't, or they should both not have "var" before (that is, if "var string" has been done elsewhere).

    It's practically impossible to tell what this is supposed to be doing without some sample data. But, this looks pretty dodgy:

    Code javascript:
    string += itemRepeat = + " | ";

    The = + can't be right - it doesn't make sense. You should be using firebug to see if it spits out any errors in the console.


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
  •