I can't remove an itme completely froman object. I tried:
function randomKey(obj) {
var ret;
var c = 0;
for (var key in obj)
if (Math.random() < 1/++c)
ret = key;
return ret;
}
while(aCountryNames.length > 0){
randomValue = randomKey(aCountryNames);
alert(randomValue); // 42x
delete aCountryNames[randomValue];
}
I have used an extra var to accomplish this:
var i = 42;
while(i > 0){
randomValue = randomKey(aCountryNames);
alert(randomValue); // 42x
delete aCountryNames[randomValue];
i--;
}
The first while ends up with "undefined" alerts.
The object is:
var aCountryNames = {
Alaska: 'Alaska',
NorthWestTerritory: 'North West Territory',
Alberta: 'Alberta',
Ontario: 'Ontario',
Greenland: 'Greenland',
etc.
After selecting an item (no problem with selecting, but for testing i use alert), i want to completely remove it from the object,so my first while-loop will finish when there are no items left anymore.
Thanks in advance for any answer!