1

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!

Terradon
  • 883
  • 2
  • 13
  • 33
  • `aCountryNames` is an `Object` and does not have a `.length` property, so in your first example, `while(aCountryNames.length > 0)`, you're checking `undefined > 0` which is always `false`. – Noah Freitas Aug 22 '15 at 20:37
  • Are you just trying to iterate through the keys of `aCountryNames` in a random order? – Noah Freitas Aug 22 '15 at 21:05

3 Answers3

1

I tried this code :

var data = { a:"a", b:"b" };
delete data["a"];

And it worked, the attribute a has been deleted. I think you can't remove an item from an object by an index, for example : delete data[0]

You have to indicate the name of the attribute you want to delete.

  • i already did that by delete aCountryNames[randomValue]; it does not COMPLETELY remove the item. it makes its property falase or something like that (according other posts on this site) – Terradon Aug 22 '15 at 21:03
  • correction: i do use that part of course. My problem was the misunderstanding of object.length – Terradon Aug 22 '15 at 21:14
1

As Noah Freitas has pointed out, an Object has no length property.

You should replace

aCountryNames.length

for

Object.keys(aCountryNames).length

Maybe you want to have a look to this post about calculating the length of an object.

EDIT:

Here I leave you the snippet:

var aCountryNames = {   
 Alaska: 'Alaska',
 NorthWestTerritory: 'North West Territory',
 Alberta: 'Alberta',
 Ontario: 'Ontario',
 Greenland: 'Greenland'
}

function randomKey(obj) {
 var ret;
 var c = 0;
 for (var key in obj)
     if (Math.random() < 1/++c)
         ret = key;
 return ret;
}

while(Object.keys(aCountryNames).length > 0){
        randomValue = randomKey(aCountryNames);
        delete aCountryNames[randomValue];
        alert(randomValue + '; there are ' + Object.keys(aCountryNames).length + ' countries left.');
}

Hope it helps!

Community
  • 1
  • 1
David
  • 6,695
  • 3
  • 29
  • 46
  • Thank you!! especially because of the simplicity and the provided link! This solution works like a champ:) (and i understand it too...) – Terradon Aug 22 '15 at 21:08
1

As JeanMel pointed out you can't delete an item by referencing a numerical index like you would with an array. You'll need to delete the item using it's key.

If you're trying to delete a random item from the object you could do the something similar to the following:

function randomItem(obj) {
  var result,
    count = 0;
  for (var prop in obj)
    if (Math.random() < 1/++count)
       result = prop;
  return result;
}

var aCountryNames = {   
  Alaska: 'Alaska',
  NorthWestTerritory: 'North West Territory',
  Alberta: 'Alberta',
  Ontario: 'Ontario',
  Greenland: 'Greenland'
  // Additional items
}

delete aCountryNames[randomItem(aCountryNames)];

The randomItem() function will return a random key from the object. Using delete aCountryNames[randomItem(aCountryNames)] will delete the item from aCountryNames.

Community
  • 1
  • 1
Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184