Reputation: 321
FIrstly, no DOM, this is on the serverside. My question is pretty much identical to: Elegant technique to move items from one array to another Sadly in Javascript there are no queues.
I have a bunch of cards represented as objects in an array called Deck.
I want to move from the Deck to the hand in a clean way. I can use splice and concat to the hand array.
But I would not like to do that all the time. Ideally I would have a function that I can pass in the card object and the destination array:
function moveCard (deck[5], hand) {
// return success
}
Or is there a different structure that can better model the data in JS?
Thanks.
Upvotes: 0
Views: 907
Reputation: 56
If you'd prefer to do this as a queue you can use Array.push and Array.shift to use arrays as queues.
For example:
var x = [];
x.push("a"); // x = [ "a" ]
x.shift(); // returns "a", x = []
x.push("a"); // x = [ "a" ]
x.push("b"); // x = [ "a", "b" ]
x.shift(); // returns "a", x = [ "b" ]
x.shift(); // returns "b", x = []
You could just shift from the deck and push to the hand.
Upvotes: 2