1

hello and thank every one. Updated link on kopy.io.

var gems = [...,

    {
      id: 3,
      name: 'Level-3',
      row: {
        r1: '*-*-*',
        r2: '**-**',
        r3: '*-*-*'
        },
      canPushtoDb: true,
      hideLevel: true,
      status: 1
      //canvas
    }    

  ];

Now i'm trying to iterate over some object like this:

    var text;
    var arr;
    var arr2 = gems[0].row;


for( arr in arr2){
    text += '<br />' + arr2[arr] + '<br />';
}

But is not working completly:

enter image description here

//* If i will do a kind of graphic map:
// var something = * * * * *  <--- row1
//                 * * * * *  <--- row2
//                 * * * * *  <--- row3
// somthing = [x][y]

So i'm keeping things on gems[] array, easy to edit/change/modify. Thats realy the problem because in other way i'd must to implement a kind of function on each level. I need a way to iterate over 'row' member after check on 'id' or 'name' key value.

I'm confused because i can acces individualy on each member like: gems[2].name ---> Level-3 or gems[1].row.r2 ---> -***-. But it is not what i want. I searching for a loop that after check on some 'value' iterate trought 'row' member. for example: if level == id then loop on gems[i].row. But i can't find the way.

var gems = [{
    id: 1,
    name: 'Level-1',
    row: {
      r1: '*****',
      r2: '-***-',
      r3: '--*--'
    },
    canPushtoDb: true,
    hideLevel: false,
    status: 1
      //canvas
  }, {
    id: 2,
    name: 'Level-2',
    row: {
      r1: '**-**',
      r2: '-*-*-',
      r3: '-***-',
    },
    canPushtoDb: true,
    hideLevel: false,
    status: 1
      //canvas
  }, {
    id: 3,
    name: 'Level-3',
    row: {
      r1: '*-*-*',
      r2: '**-**',
      r3: '*-*-*'
    },
    canPushtoDb: true,
    hideLevel: true,
    status: 1
      //canvas
  }

];


var text;
var arr;
var arr2 = gems[0].row;


for (arr in arr2) {
  text += '<br />' + arr2[arr] + '<br />';
}
document.write(text);

2
  • 1
    before text += ..., did you define text like this : var text = ""? If not, this is your undefined at the beginning of text. Commented Nov 30, 2016 at 10:30
  • lol. Thank you very much. That was it. Commented Nov 30, 2016 at 10:43

2 Answers 2

1

Why not make row property to an array, which is easily iterable, at least better than an object with properties?

row: ['*-*-*', '**-**', '*-*-*'],

If you like to keep the object, you could iterate over the keys

['r1', 'r2', 'r3'].forEach(function (k) {
    console.log(gems[1].row[k]) //do something 
});
Sign up to request clarification or add additional context in comments.

2 Comments

And if you don't know all the key in your object : Object.keys(myObject).forEach(...)
Thanks Nina, it works as my first loop showed on the post. The output is the same as you can saw in the image above. Perhaps is my fault, but i can't see why undefined it is showing again. But your aproach resolve a warning in the editor point me to wrap the function on an ifstatement. So thanks any way. And thanks again because i can saw your are accesing on second member likerow[j], thats explain me more!!!!!!.
1

Your gems[0].row returns a object and not an array.

Hence what you want is to loop through a object. Like

var arr2 = gems[0].row;

for (var key in arr2 ) {
  text += '<br />' + arr2[key] + '<br />';
}

3 Comments

Conceptualy you are right. because i was using an arr variable with some confusing meaning, but was nothing more that a 'declared' variable, it wasn't initialized!. Thanks any way!
I like this aproach for simplicity, but i preffer @Nina's aproach because it avoid the editor warning.
@Hell0 no problem. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.