Friday 15 January 2010

How to load hash in a loop using objects as keys in JavaScript? -


I get an unexpected behavior when trying to load items as keys; That is, the hash later refers to the last key used always after retrieving my data. I hope to be due to the behavior of this closing, however, I thought what would be necessary to stop it:

  var hash = {}; Var arry = []; Var list = [{val: "a"}, {val: "b"}, {val: "c"}]; Var len = list.length; Dump ("load: \ n"); (Let i = 0; i & lt; len; i ++) for {let pos = i; Obj = List [POS]; Hash [obj] = obj.val; Arry.push (obj); Dump ("+ + obj.val +" "+ hash [obj] + \"); } Dump ("Retrieve: \ n"); (Let i = 0; i & lt; len; i ++) for {let pos = i; Obj = List [POS]; Dump ("+ + obj.val +" "+ arry [pos] .val +" + hash [obg] + "\ n");}   

Output is:

  load: aabbcc retrieve: aacbbcccc   

I am trying to intentionally let's stop this by increasing the scope of repeat items running, even if I apparently But I'm missing out on something. I want to understand the reason behind it and how to use it javascript.

< P> This means that if you pass something like that If you are not using an item that does not "override", they all have the same string representation (this is "[object object]" This is the reason why hashes always always used the last key.

And here:

No comments:

Post a Comment