2016-09-15 69 views
0

我错过了一个简单的语法规则,所以也许你可以指出我。Javascript索引对象内的数组

var board = { 
    //this data structure was heavily discussed with Lydia Brothers. its current form is due to her help 
    state: { 
    flipped: true, 
    ballWasDroppedLastMove: false, 
    topPlace: 0, 
    bottomPlace: 0, 
    top: [[1,2,3,4,5,6],[7,8,9,10,11,99],[12,13,14,15,99,99],[16,17,18,99,99,99],[19,20,99,99,99,99],[21,99,99,99,99,99]], 
    bottom: [[0,0,0,0,0,0],[0,0,0,0,0,99], [0,0,0,0,99,99], [0,0,0,99,99,99], [0,0,99,99,99,99], [0,99,99,99,99,99]], 
    }, ... 

的我做对象的某些操作定义为跟随对象... - 尤其是我感兴趣的board.state.top

当我打印board.state.top到控制台我得到类似下面的图片...

enter image description here

我要访问的值12,11,0,0,99,99

我从其他语言的经验告诉我,我应该做这样的事情...

for (i=0; i<6; i++){ 
    console.log(pboard.state.top[i]) 
} 

...,这就是我究竟是如何得出上述形象。我试过类似board.state.top[i][j](增加额外的尺寸),但打印的值0,0,0,0,99,99

如何访问这些元素?

如下的建议,我尝试了以下(无范围)...

var i; 
var j; 
for (i=0; i<6; i++){ 
    row = pboard.state.top[i]; 
    row.forEach(element => {console.log(element);}); 
    // for (j=0; j<6; j++){ 
    // console.log(top[j]) 
    // } 
} 
+0

没有值'12,11,0,0 ,你已经向我们展示了99,99'? – Bergi

+0

您发布的屏幕截图[建议](http://stackoverflow.com/q/4057440/1048572)[您正在查看该对象太早](http://stackoverflow.com/q/23392111/1048572) – Bergi

+0

'board.state.top [i] [j]'应该可以工作......也许你已经混淆了索引订单?尝试'board.state.top [j] [i]' – david

回答

0

为了简化问题,做一个系统的订单。

首先从属性board.state.top检索array并将其存储在一个变量中。

然后使用forEach API遍历元素。

实施例:

var board = { 
 
    //this data structure was heavily discussed with Lydia Brothers. its current form is due to her help 
 
    state: { 
 
    flipped: true, 
 
    ballWasDroppedLastMove: false, 
 
    topPlace: 0, 
 
    bottomPlace: 0, 
 
    top: [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 99], [12, 13, 14, 15, 99, 99], [16, 17, 18, 99, 99, 99], [19, 20, 99, 99, 99, 99], [21, 99, 99, 99, 99, 99]], 
 
    bottom: [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 99], [0, 0, 0, 0, 99, 99], [0, 0, 0, 99, 99, 99], [0, 0, 99, 99, 99, 99], [0, 99, 99, 99, 99, 99]], 
 
    } 
 
} 
 

 
var that_row = board.state.top[2]; 
 
that_row.forEach(element => { console.log(element); });

+0

我对帖子进行了修改。我尝试了你的建议,但同样的事情 - 打印0和99's –

+0

@KendallWeihe我检查了更新后的帖子。 那么我从哪里得到期望值'12,11,0,0,99,99'?您的新阵列没有任何价值11 –

+0

检查该帖子的评论,这已得到解决。谢谢! –

0

尝试此。它应该工作。您需要达到该值,然后逐个获得所需的值。像这样的东西

state.top.forEach(function(innerItem){ 
    innerItem.forEach(function(item){ 
     console.info(item); 
    }) 
}) 

希望它有帮助。

快乐学习:)

+0

WTH是'&& state.top.length && state.top.length> 0'应该是好的吗? – Bergi

+0

容易的男孩!你可以跳过它。我在小提琴中这样做,只是检查值是否存在及其数组。不会伤害你拥有它。 – Vatsal

+0

是的,你真的应该跳过它。如果你想检查一个数组,只需使用'if(Array.isArray(state.top))' - 这就够了。 – Bergi

0

后续片段将在POS财产打印每个值吧数组:

var board = { 
 
    //this data structure was heavily discussed with Lydia Brothers. its current form is due to her help 
 
    state: { 
 
    flipped: true, 
 
    ballWasDroppedLastMove: false, 
 
    topPlace: 0, 
 
    bottomPlace: 0, 
 
    top: [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 99], [12, 13, 14, 15, 99, 99], [16, 17, 18, 99, 99, 99], [19, 20, 99, 99, 99, 99], [21, 99, 99, 99, 99, 99]], 
 
    bottom: [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 99], [0, 0, 0, 0, 99, 99], [0, 0, 0, 99, 99, 99], [0, 0, 99, 99, 99, 99], [0, 99, 99, 99, 99, 99]], 
 
    } 
 
} 
 

 
board.state.top.forEach(function(pos, topIndex) { 
 
    pos.forEach(function(v, i) { 
 
     console.log('From top index', topIndex, 'value in position', i, v); 
 
    }); 
 
});

+0

没有运气,我在想程序中的其他地方有代码阻止了这个工作。为什么它会在控制台上打印不同的值?请查看此处的完整代码:http://codepen.io/garyyo/pen/mAdjVG?editors=1111 –

+0

我决定只在代码片段中留下非ES6代码,因为可能您的环境不支持它。但是,console.log只是引导你了解正在发生的事情。我给了你打印每个位置和每个位置值的代码。现在你可以用它做你想做的事。 –