3 Replies - 473 Views - Last Post: 11 December 2012 - 07:49 AM

#1 jolly75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-December 12

Javascript resetting an array

Posted 10 December 2012 - 08:01 AM

I'm having with some very strange behaviour when I try to reset an array; eg:

data.length=0;


where data is an array. I'll try and include only the relevant code here but basically what I;m doing is on each iteration of my program I'm populating the array with updated values and the array then is used in another function. But when I reset the array the function appears to get the values on the first iteration but none afterwards.

However when I don't reset the array then the function gets the values, but also the older values. I don't want this, as I only want the new updated values only. This is a code snippet:


var buffer = [['1',[0]],['2',[0]],['3',[0]],['4',[0]]];
          var dataset = [];

             ws.onmessage = function(evt){ 
  	         dataset.length=0;
        	 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);
        	        
        	        dataset.push(buffer[i][1][0]);
        	        draw();
        	    	//console.log(dataset); 
                        
     	        }

        }


The function uses the dataset array to redraw a chart.

I've tried to keep it simple here, but the full function is http://jsfiddle.net/2NXF2/58/.

I really don't know what's causing this unexpected behaviour.




console.log(dataset) shows the new updated values, but somehow dataset.length=0; is preventing the updated array to be used by the draw() function. As without the resetting of the array the array can be used by the draw() function.



I've tried to not reset the array but instead get the last 4 elements and put them in a new array and then send them to the draw(), but still the same odd behaviour:



Please, Please help

    	    x = dataset.slice(-4);
        	    console.log(x);
        	    draw();


But if I don't do that or don't clear the array, then draw() render a 'wrong' chart. I can't see what is wrong.

THE problem somehow seems to reside with the resetting of the array and that because of this it means the draw() function appears to be called only once at the first iteration.

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript resetting an array

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Javascript resetting an array

Posted 10 December 2012 - 08:03 AM

Hello there,

You don't reset an array by setting it's length. That's like having a bus full of people, you don't empty it by crushing the bus to size, remove the people first.

The easier way to do it would be data = [];. So, basically overwrite the array with an empty one.
Was This Post Helpful? 1
  • +
  • -

#3 jolly75  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-December 12

Re: Javascript resetting an array

Posted 10 December 2012 - 08:10 AM

Hi Thanks for replying. I've tried doing that also but that does not work either and I still get the same problem.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: Javascript resetting an array

Posted 11 December 2012 - 07:49 AM

could you show what data dataset is supposed to hold and which it does?

Quote

I only want the new updated values only

resetting an array empties it, so all data are lost. maybe you’re better off with an appropriate object.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1