将两个数组组合成值对,以形成一副完整的卡片组

时间:2015-07-20 22:02:29

标签: arrays key

我正在尝试将两个阵列组合在一起制作一副看起来像这样的卡片:

ItemsSource="{Binding Source={StaticResource CollectionViewSourceCategories}, Path=View}"

....这是我到目前为止所做的:

[{card: "A", suit: "d"}, {card: "A", suit: "c"}, {card: "A", suit: "s"},    {card: "A", suit: "h"}, {card: "2", suit: "d"}.....]

2 个答案:

答案 0 :(得分:1)

使用Array.forEach,您可以执行以下操作:

var ranks = [ "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
var suits = ["d", "c", "s", "h"];
var deck= [];
suits.forEach(function(suit){
  ranks.forEach(function(rank){
    deck.push(new Card(rank, suit));
  })
});

编辑:如果您还没有写过卡片方法:

function Card(rank, suit){
  this.card = rank;
  this.suit = suit;
}

答案 1 :(得分:0)

如果组合这两个数组,您将拥有一个数组:

["A","2","3","4","5","6","7","8","9","10","J","Q","K","d","c","s","h"]

这并不代表完整的卡片组,而使用嵌入式for循环打印卡片就像现在一样,所以我不认为你只想将一个阵列附加到另一个。你能提供更多关于你想对阵列做什么的背景吗?

但是,要回答你的问题:如果你想附加两个数组,你可以使用:

var appendedArray = ranks.concat(suits);

将导致上述数组

与您更新的问题有关:您被称为"新卡(排名[j],西装[i]);"你有一个卡构造函数,这是有效的吗?如果是这样,如果构造函数与您使用它的方式匹配,则代码应该是正确的。发布构造函数的代码将有助于更新您面临的问题