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

前端 未结 8 1357
囚心锁ツ
囚心锁ツ 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:30

    You often will want to examine the particular properties of an instance of an object, without all of it's shared prototype methods and properties:

     Obj.prototype.toString= function(){
            var A= [];
            for(var p in this){
                if(this.hasOwnProperty(p)){
                    A[A.length]= p+'='+this[p];
                }
            }
    
        return A.join(', ');
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题