Q:

Extracting values from USGS real time water service

There must be something simple I am missing, but alas, I do not know what I do not know. Below is the code I have thus far for trying to get current streamflow conditions from the USGS.
// create site object
function Site(siteCode) {

    this.timeSeriesList = [];
    this.siteCode = siteCode;
    this.downloadData = downloadData;
    this.getCfs = getCfs;

    // create reference to the local object for use inside the jquery ajax function below
    var self = this;

    // create timeSeries object
    function TimeSeries(siteCode, variableCode) {
        this.variableCode = variableCode;
        this.observations = [];
    }

    // create observation object
    function TimeSeriesObservation(stage, timeDate) {
        this.stage = stage;
        this.timeDate = timeDate;
    }

    // include the capability to download data automatically
    function downloadData() {

        // construct the url to get data
        // TODO: include the capability to change the date range, currently one week (P1W)
        var url = "http://waterservices.usgs.gov/nwis/iv/?format=json&sites=" + this.siteCode + "&period=P1W&parameterCd=00060,00065"

        // use jquery getJSON to download the data
        $.getJSON(url, function (data) {

            // timeSeries is a two item list, one for cfs and the other for feet
            // iterate these and create an object for each
            $(data.value.timeSeries).each(function () {

                // create a timeSeries object
                var thisTimeSeries = new TimeSeries(
                    self.siteCode,

                    // get the variable code, 65 for ft and 60 for cfs
                    this.variable.variableCode[0].value
                );

                // for every observation of the type at this site
                $(this.values[0].value).each(function () {

                    // add the observation to the list
                    thisTimeSeries.observations.push(new TimeSeriesObservation(

                        // observation stage or level
                        this.value,

                        // observation time
                        this.dateTime
                    ));
                });

                // add the timeSeries instance to the object list
                self.timeSeriesList.push(thisTimeSeries);
            });
        });
    }

    // return serialized array of cfs stage values
    function getCfs() {

        // iterate timeseries objects
        $(self.timeSeriesList).each(function () {

            // if the variable code is 00060 - cfs
            if (this.variableCode === '00060') {

                // return serialized array of stages
                return JSON.stringify(this.observations);
            }
        });
    }
}

When I simply access the object directly using the command line, I can access individual observations using:
> var watauga = new Site('03479000')
> watauga.downloadData()
> watauga.timeSeriesList[0].observations[0]

I can even access all the reported values with the timestamps using:
> JSON.stringify(watauga.timeSeriesList[0].observations)

Now I am trying to wrap this logic into the getCfs function, with little success. What am I missing?

A:

The real issue, I discovered through just starting over from scratch on this function, is something wrong with my implementation of jQuery.().each(). My second stab at the issue, I successfully used a standard for in loop. Here is the working code.
function getCfs() {
    for (var index in this.timeSeriesList) {
        if (this.timeSeriesList[index].variableCode === '00060'){
            return JSON.stringify(this.timeSeriesList[index].observations);
        }
    }
}

Also, some of the stuff you are talking about @Palpatim, I definitely will have to look into. Thank you for pointing out these considerations. This looks like a good time to further investigate these promises things.

