Why does it do
This is completely normal. The created map object does not know how to present itself when you execute your alert() , simply because there is no implementation of the toString() method.
Solution
Try specifying an anonymous implementation of the toString() function for each map object as follows:
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);
Note
You should use console.log() instead of alert() , since it is much less annoying and easier to debug the console (press F12). But be careful with production code running under IE9 or lower, as their javascript engine will work when the development console is not open.
source share