[英]Finding sub properties of javascript object
我的代碼是這樣的:
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]);
}
}
}
我知道對此有很多疑問和答案,但我不明白為什么會得到這個結果:
[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 true。
我只想發送一個對象並搜索屬性名稱及其值。 當它找到時,我只想中斷搜索並返回我的JSON對象的一個屬性。
字符串是可枚舉的。 例如:
var str = "string"
for (var c in str) {
console.log(str[c]);
}
返回值:
s
t
r
i
n
g
該方法可以確定for ... in循環是否可以枚舉對象中的指定屬性。
如果要排除字符串,請在if語句中添加對typeof prop !== "string"
的檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.