Issues accessing an object's array values - returns null or 0s

Posted by PhatNinja on Stack Overflow See other posts from Stack Overflow or by PhatNinja
Published on 2012-09-18T14:49:43Z Indexed on 2012/09/18 15:38 UTC
Read the original article Hit count: 193

Filed under:
|
|
|
|

The function below should return an array of objects with this structure:

TopicFrequency = {
                name: "Chemistry", //This is dependent on topic
                data: [1,2,3,4,5,6,7,8,9,10,11,12] //This would be real data
            };

so when I do this:

myData = this.getChartData("line");

it should return two objects:

{name : "Chemistry", data : [1,2,3,4,51,12,0,0, 2,1,41, 31]}
{name : "Math", data : [0,0,41,4,51,12,0,0, 2,1,41, 90]}

so when I do console.log(myData); it's perfect, returns exactly this.

However when I do console.log(myData[0].data) it returns all 0s, not the values. I'm not sure what this issues is known as, and my question is simple what is this problem known as?

Here is the full function. Somethings were hardcoded and other variables (notable server and queryContent) removed. Those parts worked fine, it is only when manipulated/retreiving the returned array's values that I run into problems. Note this is async. so not sure if that is also part of the problem.

getChartData: function (chartType) {     
    var TopicsFrequencyArray = new Array(); 
        timePairs = this.newIntervalSet("Month"); 
        topicList = new Array("Chemistry", "Math");//Hard coded for now
        var queryCopy = {
            //sensitive information
        };
        for (i = 0; i < topicList.length; i++) {
            var TopicFrequency = {
                name: null,
                data: this.newFilledArray(12, 0)
            };
            j = 0;
            TopicFrequency.name = topicList[i];

            while (j < timePairs.length) {
                queryCopy.filter = TopicFrequency.name;
                //additional queryCopy parameter changes made here
                var request = esri.request({ url: server, content: queryCopy, handleAs: "json", load: sucess, error: fail });
                j = j + 1;
                function sucess(response, io) {
                    var topicCountData = 0;
                    query = esri.urlToObject(io.url);
                    var dateString = query.query.fromDate.replace("%", " ");
                    dateString = dateString.replace(/-/g, "/");
                    dateString = dateString.split(".");
                    date = new Date(dateString[0]);
                    dojo.forEach(response.features, function (feature) {
                        if (feature.properties.count > 0) {
                            topicCountData = feature.properties.count;
                        }
                        TopicFrequency.data[date.getMonth()] = topicCountData;
                    });
                }
                function fail(error) {
                    j = j + 1;
                    alert("There was an unspecified error with this request");
                    console.log(error);
                }
            }
            TopicsFrequencyArray.push(TopicFrequency);

          }
},

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about arrays