Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

console.log/console.dir showing last state of object, not current

I want to log how my array/object is changing with new steps of loop. Console.log does this bad. It shows only last state of object everywhere. For example:

var a = {};
console.log(a); // {bob1: 0, bob2: 0}
a.bob1 = 0;
console.log(a); // {bob1: 0, bob2: 0}
a.bob2 = 0;
console.log(a); // {bob1: 0, bob2: 0}

I found there, on so, another command: console.dir. It is working properly in same example. It shows states of object correcly.

Look this example. This command works perfect: http://jsfiddle.net/RLzVV/

Now, look my code pls. All output is in console. http://jsfiddle.net/3BDs7/4/

This is aStar algorithm. Take a look on this part (neighbor 3 loop <--- neighbor 3 start ---> this code is situated here <--- neighbor 3 stop -->) in console. Lanes, which output this to console are 105-113:

new openset length: 2
openset after adding new vertex
**shows 1 element, but length is 2**

It shows length is 2, but shows only 1 element. But! Seems to me algo is working correctly (it is popping another element, which is hidden on this step after). Why this bug appears? Did I did something wrong? Seems to me, everywhere only last state of array shown, not current:( help me please.

like image 793
Sharikov Vladislav Avatar asked Apr 14 '14 17:04

Sharikov Vladislav


1 Answers

That's a known issue -- sounds like you are running under Chrome.

The easiest work-around is to JSON encode the value as you log in.

console.log(JSON.stringify(a));
like image 133
Jeremy J Starcher Avatar answered Oct 03 '22 05:10

Jeremy J Starcher