查找JavaScript对象的子属性 [英] Finding sub properties of javascript object
问题描述
我的code ISE这样的:
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
.
..
....
在最后,我到达最后一个属性作为字符串。但它仍然表示,它有propertyIsEnumareble真实的。
At the end, i am reaching the last property as a string. But it still says that it has propertyIsEnumareble true .
我只是想送的对象和搜索的属性名称和它的值。当它发现我只是想打破搜索,并返回我的JSON对象的一个属性。
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.
推荐答案
字符串是枚举。例如:
var str = "string"
for (var c in str) {
console.log(str[c]);
}
返回:
s
t
r
i
n
g
<一个href=\"https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/propertyIsEnumerable\" rel=\"nofollow\">https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/propertyIsEnumerable
此方法可确定是否在对象的指定属性可以通过列举了for ... in循环
This method can determine whether the specified property in an object can be enumerated by a for...in loop
如果您想排除字符串,增加对支票的typeof道具!== if语句串
。
If you want to exclude strings, add a check for typeof prop !== "string"
in the if statement.
这篇关于查找JavaScript对象的子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!