简体   繁体   English

查找javascript对象的子属性

[英]Finding sub properties of javascript object

My code ise this: 我的代码是这样的:

var cem = { "name": "cem topkaya" };
f_PropertyBul(cem);
function f_PropertyBul(obj) {

    for (var prop in obj) {

        document.writeln(obj + " prop: " + prop + " propertyIsEnumerable:" + obj.propertyIsEnumerable(prop) + "<br/>");

        if (obj.propertyIsEnumerable(prop)) {            
            f_PropertyBul(obj[prop]);            
        }

    }
 }

I know there are a lot of question and answers about that but i didn't get why i get this result : 我知道对此有很多疑问和答案,但我不明白为什么会得到这个结果:

[object Object] prop: isim enumaret: true
cem topkaya prop: 0 enumaret: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
c prop: 0 propertyIsEnumerable: true
.
..
....

At the end, i am reaching the last property as a string. 最后,我将字符串作为最后一个属性。 But it still says that it has propertyIsEnumareble true . 但是它仍然说它具有propertyIsEnumareble true。

I just want to send an object and search an property name and its value. 我只想发送一个对象并搜索属性名称及其值。 when it found i just want to break search and return back one property of my JSON object. 当它找到时,我只想中断搜索并返回我的JSON对象的一个​​属性。

Strings are enumerable. 字符串是可枚举的。 For example: 例如:

var str = "string"
for (var c in str) {
   console.log(str[c]);
}

Returns: 返回值:

s
t
r
i
n
g

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/propertyIsEnumerable https://developer.mozilla.org/zh-CN/Core_JavaScript_1.5_Reference/Global_Objects/Object/propertyIsEnumerable

This method can determine whether the specified property in an object can be enumerated by a for...in loop 该方法可以确定for ... in循环是否可以枚举对象中的指定属性。

If you want to exclude strings, add a check for typeof prop !== "string" in the if statement. 如果要排除字符串,请在if语句中添加对typeof prop !== "string"的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM