How to loop and get the value of the keys in an object

0

I would like to loop in an object in order to capture the value of the key and the corresponding value.

For example:

var obj = {
  'Name' : 'String 1',
  'Age'  : 'String 2',
  'Key N': 'String N'
}

I would like to return something like this:

Name - > String 1
Age - > String 2
Key N - > String N

    
asked by anonymous 27.08.2017 / 21:49

1 answer

1

To display both key and value you can do so

var obj = {
  'Name' : 'String 1',
  'Age' : 'String 2',
  'Key N': 'String N'
  }
  
  Object.keys(obj).forEach(function(item){
        console.log(item +' => '+ obj[item]);
    })
    
27.08.2017 / 21:56