Skip to content Skip to sidebar Skip to footer

Iterating Over Javascript Object- Part Ii

hello and thank every one. Updated link on kopy.io. var gems = [..., { id: 3, name: 'Level-3', row: { r1: '*-*-*', r2: '**-**', r3: '

Solution 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 
});

Solution 2:

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 />';
}

Post a Comment for "Iterating Over Javascript Object- Part Ii"