0

I have some cards defined as objects within an object, e.g:

var cards = {
    s2: {suit: 4, rank: 2, name: '2 of spades'},
    s3: {suit: 4, rank: 3, name: '3 of spades'},
    //etc. 
}; 

I need them to be within an object, not an array.

I then need to create an array of certain length and populate it with cards.

Something along these lines:

var cardDeck = [];
for (i=0; i < 52, i++){
    cardDeck.push(???);
}

I tried to use for in loop and play with keys and even managed to push objects, but only {key} worked for me, unfortunately giving object containing only key value. How do I get whole objects pushed into cardDeck array?

1 Answer 1

2

you almost had, it, just loop through the objects by their keys, get each object from the key, and push it into the array

fiddle: https://jsfiddle.net/q3jaagcq/

var cards = {
    s2: {suit: 4, rank: 2, name: '2 of spades'},
    s3: {suit: 4, rank: 3, name: '3 of spades'},
    //etc. 
}; 

var cardDeck = [];

for (var key in cards) {
    var card = cards[key];
    cardDeck.push(card);
}

console.log(cardDeck);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.