我正在通过使用javascript将西装对象数组和纸牌对象数组组合在一起来创建一副纸牌(纸牌对象数组)。
我正在使用forEach循环遍历西装,并且嵌套的是卡片的映射循环。
console.log返回正确的对象以推送到新数组,但是.push()仅使用最后一个花色和最后一个卡来添加组合的对象。
我在哪里出错呢?
我尝试了多种不同的循环和方法来附加到新数组中而没有运气。 Console.log()返回正确的值,但是我无法将正确的组合对象推入新数组。
// 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);
console.log(obj);
deck.push(obj);
return obj;
});
console.log(arr);
});
console.log(deck);
答案 0 :(得分: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);
答案 1 :(得分:1)
Object.assign(x,y)
将y
的值放到x
上。您希望不理会x
,因此请使用Object.assign({}, x,y)
将属性存储在新对象中。请看下面的演示:
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},
];
const tmp = suits.reduce((acc, s) => {
return acc.concat(family.map(f => {
return Object.assign({}, f, s);
}));
}, []);
const pre = document.createElement("pre");
pre.innerHTML = JSON.stringify(tmp, null, 4);
document.body.appendChild(pre);
答案 2 :(得分:0)
如果使用you can use 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)
旁注,订购西服棍棒/钻石/心形/黑桃似乎有很强的约定(大概是布里奇的)。用英语很容易记住,因为它们是字母。