Skip to content Skip to sidebar Skip to footer

Javascript - How To Remove All Elements Of An Array Within An Array

Currently when I console.log(deckOfCards), it is returning all 52 cards, each with a suit, value, and points assigned to them. { suit: '♦', value: 'A', points: 11 } { suit: '♦

Solution 1:

You could use a single combined object to the result set. And an object for a shorter way of getting the points.

var suits = ["♦", "♣", "♥", "♠"],
    values = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"],
    cards = [],
    suit,
    value,
    points = { A: 11, J: 10, Q: 10, K: 10 };

for (suit of suits) {
   for (value of values) {
       cards.push({ suit, value, points: points[value] || value });
    }
}

function getCard() {
    return cards.splice(Math.floor(Math.random() * cards.length), 1)[0];
}

console.log(getCard());
console.log(getCard());
console.log(getCard());
console.log(cards);

Post a Comment for "Javascript - How To Remove All Elements Of An Array Within An Array"