0

I'm missing a simple syntax rule, so maybe you can point it out to me.

I have an object defined as the following...

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]],
  }, ...

The I do some operations on the object -- in particular I'm interested in board.state.top.

When I print board.state.top to the console I get something like the following picture...

enter image description here

I want to access the values 12,11,0,0,99,99.

My experience from other languages tells me I should do something like this...

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

...and that's exactly how I got the above image. I tried something like board.state.top[i][j] (adding the extra dimension) but that prints the values 0,0,0,0,99,99

How do I access those elements?

As suggested below, I tried the following (to no extent)...

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])
  // }
}
Kendall Weihe
  • 2,021
  • 4
  • 27
  • 53
  • There are no values `12,11,0,0,99,99` in the array you've shown us? – Bergi Sep 15 '16 at 01:57
  • The screenshot you posted [suggests](http://stackoverflow.com/q/4057440/1048572) that [you're looking at the object too early](http://stackoverflow.com/q/23392111/1048572) – Bergi Sep 15 '16 at 01:59
  • `board.state.top[i][j]` should work... Maybe you've confused the index orders ? try `board.state.top[j][i]` – david Sep 15 '16 at 02:01
  • @Bergi A+! Feel free to write an answer. I was calling it inside a function, and I needed to call it after the function call. – Kendall Weihe Sep 15 '16 at 02:09
  • Are you looking for something like this? `board.state.top.forEach(array => array.forEach(value => console.log(value)));` btw, looking at your example, it isn't clear where you're taking that `12, 11, 0, 0, 99, 99` from. I don't see the #11 anywhere. – rafaelbiten Sep 15 '16 at 02:16

3 Answers3

0

To simplify things, do it in a systematic order.

First retrieve the array from property board.state.top and store it in a variable.

Then iterate through the elements using the forEach API.

Example:

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); });
Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
0

Try this. It should work. You will need to reach up to the value and then get the desired value one by one. Something like this

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

Hope it helps.

Happy Learning :)

Vatsal
  • 2,068
  • 4
  • 21
  • 24
  • WTH is `&& state.top.length && state.top.length >0` supposed to be good for? – Bergi Sep 15 '16 at 02:00
  • Easy boy!!! you can skip it. I did it in fiddle so just checked if the value is present and its an array. Won't hurt you have it. – Vatsal Sep 15 '16 at 02:02
  • Yes, you really should skip it. If you want to check for an array, just use `if (Array.isArray(state.top))` - that's enough. – Bergi Sep 15 '16 at 02:05
0

The follow snippet will print each value for it array in pos property:

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);
    });
});
Diego ZoracKy
  • 2,227
  • 15
  • 14
  • No luck, I'm thinking there is code elsewhere in the program that is stopping this from working. Why would it print different values like that to the console? Check out the full code here: http://codepen.io/garyyo/pen/mAdjVG?editors=1111 – Kendall Weihe Sep 15 '16 at 02:00
  • I decided to leave just the non ES6 code at the snippet, because maybe you are on an environment without support to it. But, the console.log is just to guide you through what is happening. I gave you the code to print every position and every value of the position. Now you can use it do what you want. – Diego ZoracKy Sep 15 '16 at 02:05