如何在JavaScript中存储和访问数组中的信息?

时间:2015-10-12 23:11:40

标签: javascript arrays

简短问题:如何访问数组每个属性内的信息?

很长的解释:我是新手编码并试图创建一个允许你玩黑杰克的应用程序。我目前正在尝试访问每张卡的价值并努力寻找方法。我可以访问当前设置中的值吗?怎么样?有没有更好的方法来组织我的数据?

感谢。

var deck = [
  {card: "Two of Clubs", value: 2},
  {card: "Three of Clubs", value: 3},
  {card: "Four of Clubs", value: 4},
  {card: "Five of Clubs", value: 5},
  {card: "Six of Clubs", value: 6},
  {card: "Seven of Clubs", value: 7},
  {card: "Eight of Clubs", value: 8},
  {card: "Nine of Clubs", value: 9},
  {card: "Ten of Clubs", value: 10},
  {card: "Jack of Clubs", value: 10},
  {card: "Queen of Clubs", value: 10},
  {card: "King of Clubs", value: 10},
  {card: "Ace of Clubs", value: 11},
  {card: "Two of Hearts", value: 2},
  {card: "Three of Hearts", value: 3},
  {card: "Four of Hearts", value: 4},
  {card: "Five of Hearts", value: 5},
  {card: "Six of Hearts", value: 6},
  {card: "Seven of Hearts", value: 7},
  {card: "Eight of Hearts", value: 8},
  {card: "Nine of Hearts", value: 9},
  {card: "Ten of Hearts", value: 10},
  {card: "Jack of Hearts", value: 10},
  {card: "Queen of Hearts", value: 10},
  {card: "King of Hearts", value: 10},
  {card: "Ace of Hearts", value: 11},
  {card: "Two of Spades", value: 2},
  {card: "Three of Spades", value: 3},
  {card: "Four of Spades", value: 4},
  {card: "Five of Spades", value: 5},
  {card: "Six of Spades", value: 6},
  {card: "Seven of Spades", value: 7},
  {card: "Eight of Spades", value: 8},
  {card: "Nine of Spades", value: 9},
  {card: "Ten of Spades", value: 10},
  {card: "Jack of Spades", value: 10},
  {card: "Queen of Spades", value: 10},
  {card: "King of Spades", value: 10},
  {card: "Ace of Spades", value: 11},
  {card: "Two of Diamonds", value: 2},
  {card: "Three of Diamonds", value: 3},
  {card: "Four of Diamonds", value: 4},
  {card: "Five of Diamonds", value: 5},
  {card: "Six of Diamonds", value: 6},
  {card: "Seven of Diamonds", value: 7},
  {card: "Eight of Diamonds", value: 8},
  {card: "Nine of Diamonds", value: 9},
  {card: "Ten of Diamonds", value: 10},
  {card: "Jack of Diamonds", value: 10},
  {card: "Queen of Diamonds", value: 10},
  {card: "King of Diamonds", value: 10},
  {card: "Ace of Diamonds", value: 11}
]

function draw(i) {
  var drawn = i[Math.floor(Math.random()*i.length)];
  console.log(drawn);
}

var playerCard1 = draw(deck);
var dealerCard1 = draw(deck);
var playerCard2 = draw(deck);
var dealerCard2 = draw(deck);

var player = playerCard1.value + playerCard2.value;
console.log(player);

4 个答案:

答案 0 :(得分:0)

你的绘制方法需要返回一些东西。

Changing the sparsity structure of a csr_matrix is expensive. 
lil_matrix is more efficient.

答案 1 :(得分:0)

首先:

你的套牌应该是这样的

var deck = [
{"card": "Two of Clubs", "value": 2},
{"card": "Three of Clubs", "value": 3},
]

如果您注意到,单词card和value在引号中

下:

在函数draw(),

return drawn

最后:

var player = playerCard1['value'] + playerCard2['value']

答案 2 :(得分:0)

您应该考虑一种更有效的方法来设置此数据。

我可以想到一个功能,用于检索卡片给出的值(我还没有测试过!):

function getCardValue( number, symbol ) {
var currCard;
   for(var i=0; i < deck.length ; ++i) {
   currCard = deck[i];
       if(currCard.card == number + " of " + symbol )
       return currCard.value;
   }
};

// you use like this
getCardValue("eight", "diamonds"); // 8

它有效,但对于一个简单的问题来说太麻烦了。此外,函数接收值int s会更好。另一种选择,因为一副牌具有符号/数字的层次结构,可以是:

var deck = { // 
    clubs: [ // array
        {value:2} // 0
        {value:1} // 1
        {value:4} // 2
        ...
        {value:3} // 11 = king
    ],
    spades: [...],
    hearts: [...],
    diamonds: [...]
}
// you access a value like this
deck.clubs[11].value // 3

用于返回基于类似&#34;八个黑桃&#34;的字符串的值。你必须编写一个函数,从字符串中提取数字和符号作为参数,然后返回deck[symbol][number].value

还有其他几种方法可以执行此操作。

答案 3 :(得分:-1)

只需访问数组元素的value属性。

例如,让我们说你想要获得&#34;六个俱乐部&#34;

的价值
var sixOfClubsVal = deck[4].value