-2

Problem :

obj = {
 module1 : {type :'int' , value : 100 },
  module2 : {type :'str' , value : 'bio' }
  module3 : {type :'boolean' , value : 'true' }
}

how to print the object name/ object property

eg : expected string " module1 module2 module3"

Please consider adding a comment when voting down so that the question can be improved. Thanks.

praveenpds
  • 2,936
  • 5
  • 26
  • 43

3 Answers3

0

You can use Object.keys()

var array = Object.keys(obj);
var string = array.join(' ')

Demo: Fiddle

Note: May need to include the polyfill for older browsers

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
-1

Duplicate question How to list the properties of a JavaScript object In modern browsers (IE9+, FF4+, Chrome5+, Opera12+, Safari5+) you can use the built in Object.keys method:

var obj = {
    module1 : {type :'int' , value : 100 },
    module2 : {type :'str' , value : 'bio' },
    module3 : {type :'boolean' , value : 'true' }
}
var keys = Object.keys(obj);

The above has a full polyfill but a simplified version is:

var getKeys = function(myobj){
   var keys = [];
   for(var key in myobj){
      keys.push(key);
   }
   return keys;
}

Alternatively replace var getKeys with Object.prototype.keys to allow you to call .keys() on any object. Extending the prototype has some side effects and I wouldn't recommend doing it.

Community
  • 1
  • 1
Bhavesh B
  • 1,121
  • 12
  • 25
-2

Try this

for (var prop in obj) {
    obj[prop] = 'xxx';
 }
Swapnil Deo
  • 241
  • 1
  • 3
  • 16