3 回答
TA贡献2019条经验 获得超9个赞
尝试Object.assign({}, x, y)代替Object.assign(x, y)。当前,您正在通过将y的所有属性添加到x中来操纵它。
// Deck of Cards
var suits = [
{suit: "clubs",color: "black"},
{suit: "spades",color: "black"},
{suit: "hearts",color: "red"},
{suit: "diamonds",color: "red"}
];
var family = [
{name: "2",value: 2},
{name: "3",value: 3},
{name: "4",value: 4},
{name: "5",value: 5},
{name: "6",value: 6},
{name: "7",value: 7},
{name: "8",value: 8},
{name: "9",value: 9},
{name: "10",value: 10},
{name: "J",value: 10},
{name: "Q",value: 10},
{name: "K",value: 10},
{name: "A",value: 1},
];
var deck = new Array();
suits.forEach(function(x){
var arr = family.map( (y) => {
var obj = Object.assign({}, x, y);
deck.push(obj);
return obj;
});
});
console.log(deck);
TA贡献1783条经验 获得超4个赞
如果可以使用flatmap,那么它可以变得非常简单:
const suits = [{ suit: "clubs", color: "black" }, { suit: "spades", color: "black" }, { suit: "hearts", color: "red" }, { suit: "diamonds", color: "red" }]
const family = [{ name: "2", value: 2 }, { name: "3", value: 3 }, { name: "4", value: 4 }, { name: "5", value: 5 }, { name: "6", value: 6 }, { name: "7", value: 7 }, { name: "8", value: 8 }, { name: "9", value: 9 }, { name: "10", value: 10 }, { name: "J", value: 10 }, { name: "Q", value: 10 }, { name: "K", value: 10 }, { name: "A", value: 1 }]
const deck = suits.flatMap(s => family.map(f => ({...s, ...f})))
console.log(deck)
附带说明一下,订购西服杆/钻石/心形/黑桃似乎有很强的约定(大概是布里奇的惯例)。用英语很容易记住,因为它们是字母。
添加回答
举报