获取 JavaScript 对象键列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3068534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting JavaScript object key list
提问by user160820
I have a JavaScript object like
我有一个 JavaScript 对象,例如
var obj = {
key1: 'value1',
key2: 'value2',
key3: 'value3',
key4: 'value4'
}
How can I get the length and list of keys in this object?
如何获取此对象中键的长度和列表?
回答by Anurag
回答by zed_0xff
var obj = {
key1: 'value1',
key2: 'value2',
key3: 'value3',
key4: 'value4'
};
var keys = [];
for (var k in obj) keys.push(k);
alert("total " + keys.length + " keys: " + keys);
回答by mikebridge
Underscore.js makes the transformation pretty clean:
Underscore.js 使转换非常干净:
var keys = _.map(x, function(v, k) { return k; });
Edit: I missed that you can do this too:
编辑:我错过了你也可以这样做:
var keys = _.keys(x);
回答by cryo
If you only want the keys which are specific to that particular object and not any derived prototypeproperties:
如果您只想要特定于该特定对象的键而不是任何派生prototype属性:
function getKeys(obj) {
var r = []
for (var k in obj) {
if (!obj.hasOwnProperty(k))
continue
r.push(k)
}
return r
}
e.g:
例如:
var keys = getKeys({'eggs': null, 'spam': true})
var length = keys.length // access the `length` property as usual for arrays
回答by Jim Nolan
obj = {'a':'c','b':'d'}
You can try:
你可以试试:
[index for (index in obj)]
this will return:
这将返回:
['a','b']
to get the list of keys or
获取密钥列表或
[obj[index] for (index in obj)]
to get the values
获取值
回答by Mohan Gundlapalli
var keys = new Array();
for(var key in obj)
{
keys[keys.length] = key;
}
var keyLength = keys.length;
to access any value from the object, you can use obj[key];
要访问对象中的任何值,您可以使用 obj[key];
回答by Sandro
Anurags answer is basically correct.
But to support Object.keys(obj)in older browsers as well you can use the code below that is copied from
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys. It adds the Object.keys(obj)method if it's not available from the browser.
Anurags 的回答基本上是正确的。但是为了支持Object.keys(obj)旧浏览器,您可以使用下面从https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys复制的代码
。Object.keys(obj)如果该方法在浏览器中不可用,它会添加该方法。
if (!Object.keys) {
Object.keys = (function() {
'use strict';
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 = [], prop, i;
for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
}());
}
回答by Jordi
Recursive solution for browsers that support ECMAScript 5:
支持 ECMAScript 5 的浏览器的递归解决方案:
var getObjectKeys = function(obj) {
var keys = Object.keys(obj);
var length = keys.length;
if (length !== 0) {
for (var i = 0; i < length; i++) {
if (typeof obj[keys[i]] === 'object') {
keys[keys[i]] = getObjectKeys(obj[keys[i]]);
}
}
}
return keys;
};
回答by bajran
var obj = {
key1: 'value1',
key2: 'value2',
key3: 'value3',
key4: 'value4'
}
console.log(Object.keys(obj));
console.log(Object.keys(obj).length)
回答by jshaw3
Use Object.keys()... it's the way to go.
使用Object.keys()......这是要走的路。
Full documentation is available on the MDN site linked below:
MDN 站点上提供了完整的文档,链接如下:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

