6

I have the following code and I want to make the deck array full of 52 different cards. Whenever I run the code and the card object is alerted it displays as '[object Object]'.

Can someone explain to me why it does this and a solution for this problem?

var suits = ["Clubs", "Diamonds", "Hearts", "Spades"];
var ranks = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"];
var deck = []; 

for (var i = 0; i < suits.length; i++) {
    for (var j = 0; j < ranks.length; j++) {
        var card = {rank: ranks[j], suit: suits[i]};
        deck.push(card);
        alert(card)
    }
}
Tim
  • 3,191
  • 2
  • 16
  • 22
  • You can alert `card.rank` or `card.suit` but not `card`. – DevlshOne Jul 16 '13 at 19:06
  • possible duplicate of [how to alert javascript object](http://stackoverflow.com/questions/3580754/how-to-alert-javascript-object) or [Print content of JavaScript object?](http://stackoverflow.com/questions/1625208/print-content-of-javascript-object) – Bergi Jul 16 '13 at 19:31

5 Answers5

6

Why it does this

This is perfectly normal. The card object you create doesn't know how to represent itself when you do your alert(), simply because there are no toString() method implementation.

Solution for your problem

Try specifying an anonymous toString() function implementation to each card object like this:

var suits = ["Clubs", "Diamonds", "Hearts", "Spades"];
var ranks = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"];
var deck = []; 

for (var i = 0; i < suits.length; i++) {
    for (var j = 0; j < ranks.length; j++) {

        var card = {
                       rank: ranks[j], 
                       suit: suits[i],
                       toString : function() { return this.rank + ' ' + this.suit; }
                   };

        deck.push(card);

        //alert(card); // console.log doesn't block code execution
        console.log(card.toString());
    }
}

Note

You should consider using console.log() instead of alert() as it is much less annoying and easier to debug in console (hit F12). But be careful with production code running IE9 or lower as their javascript engine will crash when the developper console is not opened.

Matthew Perron
  • 6,201
  • 2
  • 21
  • 24
2

This happens because you specify the entire object to alert, and alert does not know which properties are relevant. If you want an expandable view of your object, you can use console.log(card), this will output your object as a tree-view into the browser developer console.

Maxim Kumpan
  • 2,545
  • 2
  • 19
  • 23
0

That is how an object is showed, its toString method outputs just that, to see its contents use

alert(JSON.stringify(card));
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
  • Disclaimer: JSON.stringify is great for debugging when you have a fairly new browser. A disappointing number of publically-used browsers don't support it though, so just be sure to have fallbacks, or remove that logging for production code. – Katana314 Jul 16 '13 at 19:09
0

FIDDLE DEMO

Replace your alert with this-->alert(JSON.stringify(card))

var suits = ["Clubs", "Diamonds", "Hearts", "Spades"];
var ranks = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"];
var deck = []; 

for (var i = 0; i < suits.length; i++) {
    for (var j = 0; j < ranks.length; j++) {
        var card = {rank: ranks[j], suit: suits[i]};
        deck.push(card);
       alert(JSON.stringify(card))//CHANGE THIS...
       console.log(JSON.stringify(card))
    }
}

EXPLANATION

JSON.stringify converts JavaScript data structures into JSON text.Json text is nothing but key:value pair of text.It cant get simpler.

HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
0

You should change your alert to this:

alert(JSON.stringify(card))
jh314
  • 27,144
  • 16
  • 62
  • 82