1

I've been teaching myself OOJS by creating a blackjack game. Everything is going great, but since I randomly create cards, sometimes I create and then deal the same card twice in a round. I'm trying to avoid that by writing some logic that gets rid of duplicate cards.

So I found this discussion.

https://stackoverflow.com/a/840849/945517

and the code below:

function eliminateDuplicates(arr) {
  var i,
      len=arr.length,
      out=[],
      obj={};

for (i=0;i<len;i++) {
    obj[arr[i]]=0;
}
for (i in obj) {
    out.push(i);
}
return out;
}

var a=[];
var b=[];

a[0]="fish";
a[1]="fishes";
a[2]="fish";
a[3]="1";
a[4]="fishes";

b=eliminateDuplicates(a);
console.log(a);
console.log(b);

I understand what's going on in general and on almost every line except:

for (i=0;i<len;i++) {
    obj[arr[i]]=0;
}

It seems like it's looping through the array and setting the key of obj to zero. What's going on here and how does this help get rid of duplicate entries in the array being passed at the start?

Thanks!

Community
  • 1
  • 1
heyjohnmurray
  • 205
  • 3
  • 14

2 Answers2

3
{} = {key: value, key2:value2,....} 

The above is essentially a key value map. The code iterates through the array and adds the key into the map with a value of zero. When the map tries to access an existing value, all it does is reset the value to zero. A useless operation, but it avoids an if..else or other more complex logic.

Once the array is done being iterated across, you only need to iterate across the key value map to get the keys. Since the keys are guaranteed to be unique, you have a list of unique items.

yxre
  • 3,576
  • 21
  • 20
  • So in my game, I'm storing each player card in an array unique to the player. So the dealer has an array of cards and the user has an array of cards. Each card is an object that contains a point value, face value, and suit name. In the example above it's looking through an array of strings. Is the principle still the same when dealing with my cards. How much more difficult is what I'm trying to do compared to what's being done in the example? I do have close if you want to see it for some reason. – heyjohnmurray Dec 07 '14 at 05:36
  • Javascript has simple and complex data types. An object is a complex data type, and the key will be a reference to that object instead of the value of variable. You are doing it the hard way, but you are learning javascript OO principles along the way. Your code will need to be nested loop with the inner loop having a function that compares two cards and determines equality. You need an if..else, where the posted code was able to avoid it. – yxre Dec 07 '14 at 05:45
1

The main thing to realize here is that an object can only have one property per unique string, so when you set obj.fish to 0 when i = 0, you don't add a second "fish" property to obj when i = 2.

Then you can just loop through all of the properties of obj, each one is guaranteed to be unique, and thus you have stripped duplicates.

Brandon
  • 3,174
  • 1
  • 16
  • 7