1.for in 循环 并且使用hasOwnProperty 方法
1 var jsonObject1 = { 2 "name": "xiaoming", 3 "age": 29 4 }, 5 var keys1 = []; 6 for (var p1 in jsonObject1) { 7 if (jsonObject1.hasOwnProperty(p1)) 8 keys1.push(p1); 9 } 10 alert(keys1);
如果不使用 hasOwnProperty,给Object 添加自己的属性时,也会输出出来
1 Object.prototype.test = "I am test"; 2 var jsonObject = { 3 "name": "xiaoming", 4 "age": 29 5 }, 6 keys = []; 7 for (var p in jsonObject) 8 keys.push(p); 9 alert(keys); //name,age, test
时间: 2024-11-15 19:59:33