user1869421
user1869421

Reputation: 837

javascript variable is undefined in console log

My global variable 'data' gets a messsage:

 [undefined × 4] 

In the console log. That should be wrong as I've pushed 4 elements onto the array. This is the code

var data = [];

         ws.onmessage = function(evt){

         var distances = JSON.parse(evt.data);
         console.log(distances);
            for(var i=0; i<buffer.length; i++) {
                if(buffer[i][0] == distances.miles) {
                    buffer[i][1][0]++;
                    }
            }               
            console.log(buffer);

            for (var i=0; i<buffer.length; i++) { 
                data.push(buffer[i][1][0]); 
            }
            console.log(data);
            draw();//redraw the graph
            data.length=0;
        }

As you can see data should now be an array with elements.

Thanks

Upvotes: 0

Views: 1883

Answers (1)

rdiazv
rdiazv

Reputation: 1153

The console it's a live representation of the variables in memory, and you're invalidating the array's contents with data.length=0;. You should try something like console.log($.clone(data)); (from jQuery) before data.length=0;.

This is assuming that you're absolutely sure that the data you expect really exists.

Upvotes: 1

Related Questions