display object properties using for/in loop javascript

This question already has an answer here:

  • How do I loop through or enumerate a JavaScript object? 29 answers

  • This has to do with notation and syntax

    Object.property will give you undefined because you're accessing the property with the name property .

    If you have this object:

    var o = {
        property: "value",
        value: "foo"
    };
    
    o.property; // "value"
    o["property"]; // "value" (equivalent)
    o.value; // "foo"
    o["value"]; // "foo" (equivalent)
    o[o.property]; // "foo" no other notation possible
    

    So in:

    var Object = { x:1, y:2, z:3 };
    for (property in Object) {
      console.log(Object.property);
    };
    

    The value of property is "x" , "y" and then "z" . But Object.property is equivalent to Object["property"] . Whereas Object[property] gives you Object["x"] etc.

    链接地址: http://www.djcxy.com/p/28834.html

    上一篇: 带有随机密钥的Foreach JSON

    下一篇: 使用for / in循环javascript显示对象属性