简体   繁体   English

在Javascript中查找对象键

[英]Finding object keys in Javascript

I'm working on an ExtJS webapp and was looking for a way to list all of an object's own property names. 我正在研究ExtJS webapp,并且正在寻找一种方法来列出所有对象自己的属性名称。 Googling around, I quickly found some reference code on this blog . 谷歌搜索,我很快在这个博客上找到了一些参考代码。 Now, when using this keys() method, I find some strange behavior when enumerating the property names of an object of objects. 现在,当使用这个keys()方法时,我在枚举对象对象的属性名时发现了一些奇怪的行为。 Example code: 示例代码:

keys = function(obj) {
    if (typeof obj != "object" && typeof obj != "function" || obj == null) {
        throw TypeError("Object.keys called on non-object");
    }
    var keys = [];
    for (var p in obj) 
        obj.hasOwnProperty(p) && keys.push(p);
    return keys;
};

var test = {}
test["nr1"] = {testid: 1, teststr: "one"};
test["nr2"] = {testid: 2, teststr: "two"};
test["nr3"] = {testid: 3, teststr: "three"};
for (var i in keys(test)) {
    console.log(i);
}

When running this code, the console outputs: 运行此代码时,控制台输出:

0
1
2
remove()

So, on top of the expected 3 property names, it also lists a "remove()" function. 因此,除了预期的3个属性名称之外,它还列出了“remove()”函数。 This is clearly related to ExtJS, because the enumeration works as expected on a blank, non-ExtJS loading page. 这显然与ExtJS有关,因为枚举在空白的非ExtJS加载页面上按预期工作。

Can anyone explain me what exactly ExtJS is doing here? 谁能解释一下ExtJS究竟在做什么? Is there a better way to enumerate object-own property names? 有没有更好的方法来枚举对象拥有的属性名称?

Thanks a bunch, wwwald 感谢一堆,wwwald

Try to check hasOwnProperty to only list properties of the array itself, not its prototype. 尝试检查hasOwnProperty以仅列出数组本身的属性,而不是其原型。

for (var i in keys(test)) {
    if(keys(test).hasOwnProperty(i)){
      console.log(i);
    }
}

Yes, as @Thai said, not use for..in, as any array is a object and potentially could have different additions in different frameworks. 是的,正如@Thai所说,不使用for..in,因为任何数组都是一个对象,并且可能在不同的框架中有不同的添加。

keys = function(obj) {
    if (typeof obj != "object" && typeof obj != "function" || obj == null) {
        throw TypeError("Object.keys called on non-object");
    }
    var keys = [];
    for (var p in obj) 
        obj.hasOwnProperty(p) && keys.push(p);
    return keys;
};

var test = {}
test["nr1"] = {testid: 1, teststr: "one"};
test["nr2"] = {testid: 2, teststr: "two"};
test["nr3"] = {testid: 3, teststr: "three"};
document.writeln('<pre>');
document.writeln('Current method');
for (var key in keys(test)) {
    document.writeln(key);
}


document.writeln('Better method1');
for (var arr=keys(test), i = 0, iMax = arr.length; i < iMax; i++) {
    document.writeln(arr[i]);
}

document.writeln('Better method2');
Ext.each(keys(test), function(key) {
   document.writeln(key); 
});
document.writeln('</pre>');

keys(test) returns an array, so you are expected to use the classic for-init-condition-next loopm and not the for-in loop. keys(test)返回一个数组,因此你应该使用经典的for-init-condition-next loopm而不是for-in循环。

(function(arr) {
    for (var i = 0; i < arr.length; i ++) {
        console.log(i);
    }
})(keys(test));

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

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