假设我这样创建一个对象:

var myObject =
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};

检索属性名列表的最佳方法是什么?例如,我想以一些变量“键”结束,这样:

keys == ["ircEvent", "method", "regex"]

在现代浏览器(IE9+, FF4+, Chrome5+, Opera12+, Safari5+)中,您可以使用内置的Object。键的方法:

var keys = Object.keys(myObject);

上面有一个完整的填充,但一个简化的版本是:

var getKeys = function(obj){
   var keys = [];
   for(var key in obj){
      keys.push(key);
   }
   return keys;
}

或者将var getKeys替换为object. prototype.keys,以允许你在任何对象上调用.keys()。扩展原型有一些副作用,我不建议这么做。


正如slashnick指出的,您可以使用“for in”构造来遍历对象的属性名。但是,您将遍历对象原型链中的所有属性名。如果你只想遍历对象自己的属性,你可以使用object #hasOwnProperty()方法。因此有以下。

for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        /* useful code here */
    }
}

IE不支持(i in obj)原生属性。这是我能找到的所有道具的清单。

似乎stackoverflow做了一些愚蠢的过滤。

该列表可在这个谷歌组帖子的底部:- https://groups.google.com/group/hackvertor/browse_thread/thread/a9ba81ca642a63e0


我是转储功能的超级粉丝。

ajax»JavaScript变量转储在Coldfusion

下载转储功能


注意Object。Firefox 4、Chrome 6、Safari 5、IE 9及以上版本均支持key和其他ECMAScript 5方法。

例如:

var o = {"foo": 1, "bar": 2}; 
alert(Object.keys(o));

ECMAScript 5兼容性表

新方法描述


正如Sam Dutton回答的那样,ECMAScript第5版引入了一种新的方法。Object.keys()将做你想做的事情,并在Firefox 4, Chrome 6, Safari 5和IE 9中得到支持。

您也可以很容易地在不支持该方法的浏览器中实现该方法。然而,有些实现并不完全与Internet Explorer兼容。这里有一个更兼容的解决方案:

Object.keys = Object.keys || (function () {
    var hasOwnProperty = Object.prototype.hasOwnProperty,
        hasDontEnumBug = !{toString:null}.propertyIsEnumerable("toString"),
        DontEnums = [ 
            'toString', 'toLocaleString', 'valueOf', 'hasOwnProperty',
            'isPrototypeOf', 'propertyIsEnumerable', 'constructor'
        ],
        DontEnumsLength = DontEnums.length;
        
    return function (o) {
        if (typeof o != "object" && typeof o != "function" || o === null)
            throw new TypeError("Object.keys called on a non-object");
    
        var result = [];
        for (var name in o) {
            if (hasOwnProperty.call(o, name))
                result.push(name);
        }
    
        if (hasDontEnumBug) {
            for (var i = 0; i < DontEnumsLength; i++) {
                if (hasOwnProperty.call(o, DontEnums[i]))
                    result.push(DontEnums[i]);
            }   
        }
    
        return result;
    };
})();

请注意,当前接受的答案不包括检查hasOwnProperty(),并将返回通过原型链继承的属性。它也没有解释Internet Explorer中著名的DontEnum错误,即原型链上的不可枚举属性会导致本地声明的具有相同名称的属性继承其DontEnum属性。

实现Object.keys()将为您提供更健壮的解决方案。

编辑:在最近与Prototype的知名贡献者kangax讨论之后,我基于他Object.forIn()函数的代码实现了DontEnum错误的解决方案。


如果你只是想获取元素,而不是函数,那么这段代码可以帮助你

this.getKeys = function() {

    var keys = new Array();
    for(var key in this) {

        if( typeof this[key] !== 'function') {

            keys.push(key);
        }
    }
    return keys;
}

这是我的HashMap实现的一部分,我只想要键,“this”是包含键的HashMap对象


这可以在大多数浏览器中工作,甚至在IE8中,并且不需要任何类型的库。Var I是你的钥匙。

var myJSONObject =  {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; 
var keys=[];
for (var i in myJSONObject ) { keys.push(i); }
alert(keys);

在支持js 1.8的浏览器下:

[i for(i in obj)]

可以用jQuery这样做:

var objectKeys = $.map(object, function(value, key) {
  return key;
});

Mozilla有完整的实现细节,关于如何在不支持它的浏览器中实现它,如果这有帮助的话:

if (!Object.keys) {
  Object.keys = (function () {
    var hasOwnProperty = Object.prototype.hasOwnProperty,
        hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
        dontEnums = [
          'toString',
          'toLocaleString',
          'valueOf',
          'hasOwnProperty',
          'isPrototypeOf',
          'propertyIsEnumerable',
          'constructor'
        ],
        dontEnumsLength = dontEnums.length;

    return function (obj) {
      if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');

      var result = [];

      for (var prop in obj) {
        if (hasOwnProperty.call(obj, prop)) result.push(prop);
      }

      if (hasDontEnumBug) {
        for (var i=0; i < dontEnumsLength; i++) {
          if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
        }
      }
      return result;
    };
  })();
}

你可以把它包括在你喜欢的任何地方,但可能是在脚本堆栈顶部的某种extensions.js文件中。


以公认的答案为基础。

如果对象有你想要调用的属性,那么说.properties()试试!

var keys = Object.keys(myJSONObject);

for (var j=0; j < keys.length; j++) {
  Object[keys[j]].properties();
}

因为我几乎在每个项目中都使用了underscore.js,所以我会使用keys函数:

var obj = {name: 'gach', hello: 'world'};
console.log(_.keys(obj));

它的输出将是:

['name', 'hello']

Object.getOwnPropertyNames (obj)

除了Object.keys(obj)所显示的属性外,该函数还显示不可枚举的属性。

在JS中,每个属性都有几个属性,包括一个布尔枚举。

一般来说,不可枚举的属性更“内在化”,使用也更少,但有时深入研究它们以了解真正发生的事情是很有见地的。

例子:

var o = Object.create({base:0}) Object.defineProperty(o, 'yes', {enumerable: true}) Object.defineProperty(o, 'not', {enumerable: false}) console.log (Object.getOwnPropertyNames (o)) // ['yes', 'not'] console.log(种(o)) // ['yes'] For (var x in o) console.log (x) //是的,base

还要注意如何:

对象。getOwnPropertyNames和Object。钥匙不是沿着原型链往上找底座的 For in

更多关于原型链的信息,请访问:https://stackoverflow.com/a/23877420/895245


解决方案工作在我的情况和跨浏览器:

var getKeys = function(obj) {
    var type = typeof  obj;
    var isObjectType = type === 'function' || type === 'object' || !!obj;

    // 1
    if(isObjectType) {
        return Object.keys(obj);
    }

    // 2
    var keys = [];
    for(var i in obj) {
        if(obj.hasOwnProperty(i)) {
            keys.push(i)
        }
    }
    if(keys.length) {
        return keys;
    }

    // 3 - bug for ie9 <
    var hasEnumbug = !{toString: null}.propertyIsEnumerable('toString');
    if(hasEnumbug) {
        var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
            'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];

        var nonEnumIdx = nonEnumerableProps.length;

        while (nonEnumIdx--) {
            var prop = nonEnumerableProps[nonEnumIdx];
            if (Object.prototype.hasOwnProperty.call(obj, prop)) {
                keys.push(prop);
            }
        }

    }

    return keys;
};

使用Reflect.ownKeys ()

var obj = {a: 1, b: 2, c: 3};
Reflect.ownKeys(obj) // ["a", "b", "c"]

对象。键和对象。getOwnPropertyNames不能获得不可枚举的属性。它甚至适用于不可枚举的属性。

var obj = {a: 1, b: 2, c: 3};
obj[Symbol()] = 4;
Reflect.ownKeys(obj) // ["a", "b", "c", Symbol()]

在ES6及后续版本(ECMAScript 2015)中,你可以获得所有属性,如下所示:

let keys = Object.keys(myObject);

如果你想列出所有的值

let values = Object.keys(myObject).map(key => myObject[key]);

这里有很多答案…这是我的意见。

我需要一些东西来打印出所有的JSON属性,甚至是带有子对象或数组的属性(包括父名称)。

对于这个JSON:

mylittleJson = {
  "one": "blah",
  "two": {
      "twoone": "",
      "twotwo": "",
      "twothree": ['blah', 'blah']
  },
  "three": ""
}

它会打印这个:

.one
.two.twoone
.two.twotwo
.two.twothree
.three

这是函数

function listatts(parent, currentJson){ var attList = [] if (typeof currentJson !== 'object' || currentJson == undefined || currentJson.length > 0) { return } for(var attributename in currentJson){ if (Object.prototype.hasOwnProperty.call(currentJson, attributename)) { childAtts = listatts(parent + "." + attributename, currentJson[attributename]) if (childAtts != undefined && childAtts.length > 0) attList = [...attList, ...childAtts] else attList.push(parent + "." + attributename) } } return attList } mylittleJson = { "one": "blah", "two": { "twoone": "", "twotwo": "", "twothree": ['blah', 'blah'] }, "three": "" } console.log(listatts("", mylittleJson));

希望这也能有所帮助。