How to loop through Javascript Struct Values?

I have a struct like this:

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?


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;
            }
         }
      }
   }
}

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:

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

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:

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.

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.

    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;
                }
            }
        }
   }
}