Reputation: 19
I am trying to access the name key of the variable drawnCard
in this code and I cannot figure out why it does not work.
Would you mind checking out the end of my Javascript code ?
http://jsbin.com/ohinif/10/edit
Please don't mind the highly probable beginner's coding...
Thanks!!
Upvotes: 1
Views: 79
Reputation: 2227
When splicing the one element out of your array, it is still returning it as an array.
Therefore to get the name you would need to use:
var drawnCard = deck.splice(randomCard, 1);
console.log(drawnCard[0].name) // note the index here
Or you could extract the first item from the splice when pulling it out like so:
var drawnCard = deck.splice(randomCard, 1)[0]; // and the first index here
console.log(drawnCard.name)
Upvotes: 1
Reputation: 17366
Do this to your Code
var randomCard = Math.floor(Math.random() * deck.length);
console.log(randomCard)
var drawnCard = deck.splice(randomCard, 1)[0];
console.log(drawnCard.name);
DEMO Here
http://jsbin.com/ohinif/17/edit
Upvotes: 0