获取对象的键数组
我想以数组形式获取 JavaScript 对象的键,无论是在 jQuery 还是纯 JavaScript 中。
还有比这更简洁的方法吗?
var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' };
var keys = [];
for (var key in foo) {
keys.push(key);
}
I would like to get the keys of a JavaScript object as an array, either in jQuery or pure JavaScript.
Is there a less verbose way than this?
var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' };
var keys = [];
for (var key in foo) {
keys.push(key);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用
Object.keys
:这是 ES5 的一个特性。这意味着它适用于所有现代浏览器,但不适用于旧版浏览器< /a>.
ES5-shim 有一个实现
您可以窃取的 Object.keys
Use
Object.keys
:This is an ES5 feature. This means it works in all modern browsers but will not work in legacy browsers.
The ES5-shim has a implementation of
Object.keys
you can steal您可以使用 jQuery 的
$.map
。You can use jQuery's
$.map
.当然,
Object.keys()
是 <获取对象密钥的最佳方法。如果它在您的环境中不可用,则可以使用示例中的代码对其进行简单的填充(除非您需要考虑到您的循环将迭代原型链上的所有属性,这与Object.keys()
的行为)。但是,您的示例代码...
jsFiddle。
……可以修改。您可以直接在变量部分进行赋值。
jsFiddle。
当然,此行为与
Object.keys()
实际执行的操作不同 (jsFiddle)。您可以简单地使用MDN 文档上的垫片。Of course,
Object.keys()
is the best way to get an Object's keys. If it's not available in your environment, it can be trivially shimmed using code such as in your example (except you'd need to take into account your loop will iterate over all properties up the prototype chain, unlikeObject.keys()
's behaviour).However, your example code...
jsFiddle.
...could be modified. You can do the assignment right in the variable part.
jsFiddle.
Of course, this behaviour is different to what
Object.keys()
actually does (jsFiddle). You could simply use the shim on the MDN documentation.如果您在这里寻找将 n 深度嵌套对象的键列为平面数组的东西:
In case you're here looking for something to list the keys of an n-depth nested object as a flat array:
我不知道是否更简洁,但我受到启发,通过单行请求将以下内容强制到一行,但不知道它有多Pythonic;)
I don't know about less verbose but I was inspired to coerce the following onto one line by the one-liner request, don't know how Pythonic it is though ;)
摘要
要获取对象的所有键,您可以使用
Object.keys()
。Object.keys()
将一个对象作为参数并返回所有键的数组。例子:
在上面的示例中,我们将键数组存储在键 const 中。然后,我们可以通过检查键数组的长度轻松访问对象上的属性数量。
通过以下方式获取值:
Object.values()
Object.keys()
的补充函数是Object.values()
。该函数接受一个对象作为参数并返回一个值数组。例如:Summary
For getting all of the keys of an Object you can use
Object.keys()
.Object.keys()
takes an object as an argument and returns an array of all the keys.Example:
In the above example we store an array of keys in the keys const. We then can easily access the amount of properties on the object by checking the length of the keys array.
Getting the values with:
Object.values()
The complementary function of
Object.keys()
isObject.values()
. This function takes an object as an argument and returns an array of values. For example:到了 2022 年,JavaScript 仍然没有一个好的方法来处理哈希?
这会发出警告,但有效:
也就是说,扩展内置对象是有争议的。
Year 2022 and JavaScript still does not have a sound way to work with hashes?
This issues a warning but works:
That said, Extending Built-in Objects is Controversial.
如果你决定使用 Underscore.js 你最好这样做
If you decide to use Underscore.js you better do