如何在javascript中创建和填充二维数组

时间:2018-01-31 17:02:36

标签: javascript arrays

在这个我有3个数组,一个有西装,另一个有卡,第三个有数值,我必须用卡创建一个二维数组并分配一个像♠A,14这样的值

var deck = [];
          function build() {
            var suits = ["♠ ", "♥", "♣", "♦"];
           var name = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"];
           // now create a 2 dim array with each individual array representing a card.. 
            var value = [2,3,4,5,6,7,8,9,10,11,12,13,14];
           for(var i=0; i<suits.length; i++){
            for(x=0; x<name.length; x++)
            {  deck[x]=[2];
              deck.push(suits[i]+name[x]);
            }
            }
           }

1 个答案:

答案 0 :(得分:0)

这应该有效:

function build() {
    var deck = []; 
    var suits = ["♠", "♥", "♣", "♦"];
    var name = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"];
    // now create a 2 dim array with each individual array representing a card.. 
    var value = [2,3,4,5,6,7,8,9,10,11,12,13,14];
    for(var i=0; i<suits.length; i++){
        for(x=0; x<name.length; x++)
            {  
               deck.push(suits[i]+name[x]+ ',' + value[x]);
            }
        }

    return deck;
 }

现在,要获得新建甲板的价值,只需使用:

var newlyBuiltDeck = build();