I have a javascript object which is built dynamically in a loop. I'd like to replace properties that might already exist with newer ones during the iteration- basically treating the object like a Java HashSet.
Why is it that when you add a dynamic property to a javascript object, and later loop over that object with a for in loop, you get the multiple values of that property or other strange behavior?
Example:
var foo = {};
foo['bar'] = 'hello';
foo['bar'] = 'hola';
foo['bar'] = 'aloha';
foo['baz'] = 'some other thing';
var arr = [];
for(var prop in foo) { arr.push(foo[prop]) };
//2 entries (what I'd expect)
foo['bam'] = 'other other thing';
for(var prop in foo) { arr.push(foo[prop]) };
//5 entries (why?) the [bar] property should be overwritten right?
console.log(arr);
[ 'aloha',
'some other thing',
'aloha',
'some other thing',
'other other thing' ]
How does one overwrite the old value with a new one using dynamic properties?
bam, notbar, sobarwon't be modified.