0

I have a page that displays graphs based on the selected criteria. Each graph is a separate instance and needs its own object reference. I have new objects being created within the for loop but how can I access those objects outside that particular function.

var chartObject = new Array();

function runInit () {
$(document).ready(function(){
    $("#submit").click(function() { 
        $("#pointList :selected").each(function(){
            selectedValues.push($(this).val()); 
        });

        for(var j = 0; j < selectedValues.length; j++)
        {
            chartObject[j] = new Object();

            var charts = [];

            charts.push( drawDataView( demos[demo] ) );

            chartObject[j].allDataLoaded = function( ) {
                //more code             
            };
        }
    }); 
});
}

I need to use chartObject[j] in another function:

function drawDataView ( demo ) {
    var dataCache = new DataCache( chartObject[j], dataSource );
}

Obviously j isn't defined in drawDataView, so how can I create new objects within that for-loop and use the variable elsewhere?

Any suggestion is appreciated!

1 Answer 1

1

You could just pass j as an extra parameter to the drawDataView function:

function drawDataView(demo, j) {
    var dataCache = new DataCache( chartObject[j], dataSource );
}

and call it like this:

charts.push(drawDataView(demos[demo], j));

Update

Because you're not returning anything in the drawDataView function, your array will be filled with undefined. I think you want to add return dataCache; at the end.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm an idiot, thank you! Another quick question, am I creating the objects properly? Just wanted to make sure since I'm not getting the results I wanted
You're welcome. I added an update which I think will explain your unexpected results.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.