我正在尝试在 Codecademy 中学习 JavaScript,其中简要提到的一件事是“for in”循环。我在一些练习中使用过它们,但在这种情况下,我无法让它发挥作用。我可以用 for (i = blah; etc) 做到这一点,但我想知道这有什么问题以及是否有办法解决它:
//Your three dimensional array from the last exercise probably
//looked something like this:
var hands = [];
hands[0] = [ [3,"H"], ["A","S"], [1,"D"], ["J","H"], ["Q","D"] ];
hands[1] = [ [9,"C"], [6,"C"], ["K","H"], [3,"C"], ["K","H"] ];
//Loop over every dimension in the array, logging out the suit and rank
//of each card in both hands
//1. loop over each hand
for (var hand in hands) {
//2. loop over each card array in each hand
for (var card in hand) {
//3. loop over each rank/suit array for each card in each hand
for (var prop in card) {
//4. log the value of the rank/suit array item
console.log(card[prop]);
}
}
}
输出是 0 0 0,而不是数字和花色。我尝试在第一个和第二个循环之后放置 console.log() ,我注意到它在第一个循环中正常工作,但在第二个循环中没有。