-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
  • 39,722
  • 10
  • 72
  • 98
MMR
  • 2,869
  • 13
  • 56
  • 110

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
  • 57,230
  • 10
  • 89
  • 128
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
  • 7,438
  • 7
  • 46
  • 73
0
for(i in data) {
    console.log (i,':', data[i])
}
Chris Stubbs
  • 358
  • 2
  • 10