-4

I have an object like this

var data = {'name':'test','rollnum':'3','class':'10'};

I want to console it by iterating through it like,

 name:test
 rollnum:3
 class:10

Can anyone please help me.Thanks.

eko
  • 37,528
  • 9
  • 64
  • 91
MMR
  • 2,599
  • 10
  • 48
  • 105

3 Answers3

3

This will work for values that are Strings or Numbers.

var data = {'name':'test','rollnum':'3','class':'10'};

var i;
for (i in data) {
    console.log(i + ":" + data[i]);
}
Halcyon
  • 56,029
  • 10
  • 87
  • 125
0

With modern JavaScript syntax, this becomes quite elegant:

const data = {'name':'test','rollnum':'3','class':'10'};

Object.entries(data).forEach(([key, val]) => console.log(`${key}: ${val}`));
Kris Selbekk
  • 6,834
  • 7
  • 44
  • 71
0
for(i in data) {
    console.log (i,':', data[i])
}
Chris Stubbs
  • 338
  • 2
  • 10