我有一个带卡作为对象的数组:
$(document).ready(function() {
var totalPlayer = 12;
var cards = [
{"card": 2, "value": 2, "suit": "d"},
{"card": 2, "value": 2, "suit": "c"},
{"card": 2, "value": 2, "suit": "h"},
{"card": 2, "value": 2, "suit": "s"},
{"card": 3, "value": 3,...
{"card": "a", "value": 11, "suit": "s"}];
$('#butStart').click(processIt);
循环:
function processIt() {
for (var i = 0; i < cards.length; i++) {
if (cards[i].card === "a" && totalPlayer > 11) {
cards[i].value = 1;
}
else if (cards[i].card === "a") {
cards[i].value = 11;
}
}
alert(cards[50].value + cards[50].card);
}
我无法弄清楚为什么我收到错误消息“无法读取未定义的属性'卡'
答案 0 :(得分:0)
因为你的JSON中没有51个项目......?
答案 1 :(得分:0)
你必须引用你的所有数据,我已尝试过它,它就像一个魅力:
var cards = [
{"card": "2", "value": "2", "suit": "d"},
{"card": "2", "value": "2", "suit": "c"},
{"card": "2", "value": "2", "suit": "h"},
{"card": "2", "value": "2", "suit": "s"},
{"card": "3", "value": "3",...
{"card": "a", "value": "11", "suit": "s"}];
此外,您需要将数据传递给您的函数:
$('#butStart').click(processIt(cards));
function processIt(cards) {...}