How do I access properties of a javascript object if I don't know the names?

前端 未结 8 1437
囚心锁ツ
囚心锁ツ 2020-11-28 18:49

Say you have a javascript object like this:

var data = { foo: \'bar\', baz: \'quux\' };

You can access the properties by the property name:

8条回答
  •  孤独总比滥情好
    2020-11-28 19:37

    var attr, object_information='';
    
    for(attr in object){
    
          //Get names and values of propertys with style (name : value)
          object_information += attr + ' : ' + object[attr] + '\n'; 
    
       }
    
    
    alert(object_information); //Show all Object
    

提交回复
热议问题