I'm making Rummy. I need to get all objects that match two object properties, card suit
and card value
in two sets of arrays.
Array A:
[
{
suit : 'spades',
value : 13
},
{
suit : 'hearts',
value : 8
},
...
]
Array B
[
{
suit : 'spades',
value : 11
},
{
suit : 'hearts',
value : 8
},
...
]
Result would be an Array:
[
{
suit : 'hearts',
value : 8
},
]
I found this SO post on using functional programming and grouping, then using for in
to check equality:
An efficient way to get the difference between two arrays of objects?
However this seems to be based on a single property.
So I tried:
var test_hand = testHand(Control_Panel.test_hand);
var bValues = {};
test_hand.forEach(function (test_card) {
bValues[test_card.value] = test_card;
bValues[test_card.suit] = test_card;
});
var tester = this.deck.filter(function (card) {
return (card.value in bValues) && (card.suit in bValues);
});
But obviously this is going to return 2x as many cards.
Any thoughts?