0

I am trying to access one of the properties of an object that is within an array. Here is how I have it set up.

var 2002 = [{"st_as_str":"01006000102","parm_desc":"ALKALINITY, TOTAL","sub":"ALKALINITY","is_fil":false,"is_reac":false,"date":"2002-09-02","time":"16:45","field_num":70237,"lims_r":"","result":137.0,"val_qual":"","ana_meth":"E3218A","unit":"MILLIGRAM PER LITER"},

{"st_as_str":"01006000102","parm_desc":"ALKALINITY, TOTAL","sub":"ALKALINITY","is_fil":false,"is_reac":false,"date":"2002-09-08","time":"14:30","field_num":70240,"lims_r":"","result":142.0,"val_qual":"","ana_meth":"E3218A","unit":"MILLIGRAM PER LITER"}]
//These two lines are actually one.  I broke them up to make this somewhat readable. Also, this is a portion of the file.

I have been trying to access properties like this:

alert(2002[0].parm_desc);

As in: alert(array[object at this index].thisproperty) although it doesn't seem to be working. Here is more of my code.

    <script>
        function getScript(url, callback) {
           var script = document.createElement('script');
           script.type = 'text/javascript';
           script.src = url;

           script.onreadystatechange = callback;
           script.onload = callback;

           document.getElementsByTagName('head')[0].appendChild(script);
        }

        //this works and I am getting the alerts
        getScript('./analysis_valqualifi.js', function(){
            alert("Analysis Method: " + analysis_method[0][1]);
            alert("Lims_ValQualifi: " + lims_valqualifi[0][1]);
        });

        //this isn't working
        getScript('./2002_results.js', function(){
            alert(2002[0].parm_desc);
        });
    </script>

After the first 2 alerts there is a 15-20 second lag before my other scripts run so I am pretty sure JavaScript is reading the file and its my syntax that is incorrect.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user3210944
  • 57
  • 1
  • 6

2 Answers2

1

Your variable 2002 is illegal. If you attempt your first line(s) in the Chrome developer tools console directly, it immediately reports the following:

var 2002 = [{"st_as_str":"01006000102","parm_desc":"ALKALINITY, TOTAL","sub":"ALKALINITY","is_fil":false,"is_reac":false,"date":"2002-09-02","time":"16:45","field_num":70237,"lims_r":"","result":137.0,"val_qual":"","ana_meth":"E3218A","unit":"MILLIGRAM PER LITER"}, ...

SyntaxError: Unexpected number

If you simply prefix this with a character, for example:

var s2002 = [{"st_as_str":"01006000102","parm_desc":"ALKALINITY, TOTAL","sub":"ALKALINITY","is_fil":false,"is_reac":false,"date":"2002-09-02","time":"16:45","field_num":70237,"lims_r":"","result":137.0,"val_qual":"","ana_meth":"E3218A","unit":"MILLIGRAM PER LITER"}, {"st_as_str":"01006000102","parm_desc":"ALKALINITY, TOTAL","sub":"ALKALINITY","is_fil":false,"is_reac":false,"date":"2002-09-08","time":"14:30","field_num":70240,"lims_r":"","result":142.0,"val_qual":"","ana_meth":"E3218A","unit":"MILLIGRAM PER LITER"}]

this works just fine. Refer to this post for a good summary of variable name rules.

Community
  • 1
  • 1
Scott Heaberlin
  • 3,364
  • 1
  • 23
  • 22
1

Some points to consider:

Variable names cannot start with number. So change this to "data2002", for example.

Attributes in a JSON object can be numeric, but this will create gaps in your array. You don't have this issue in your code, but just put it out there.

If the attribute name as a space, e.g. {'Test Mode':true}, you can access it using the following syntax:

var testmode = obj['Test Mode'];
Schien
  • 3,855
  • 1
  • 16
  • 29