如果我不知道名称,如何访问 javascript 对象的属性?
假设你有一个像这样的 javascript 对象:
var data = { foo: 'bar', baz: 'quux' };
你可以通过属性名称访问属性:
var foo = data.foo;
var baz = data["baz"];
但是如果你不知道属性的名称,是否可以获得这些值? 这些属性的无序性质是否导致无法区分它们?
就我而言,我特别考虑这样一种情况:函数需要接受一系列名称-值对,但属性的名称可能会发生变化。
到目前为止,我对如何做到这一点的想法是将属性名称与数据一起传递给函数,但这感觉像是一个黑客。 如果可能的话,我更愿意通过内省来做到这一点。
Say you have a javascript object like this:
var data = { foo: 'bar', baz: 'quux' };
You can access the properties by the property name:
var foo = data.foo;
var baz = data["baz"];
But is it possible to get these values if you don't know the name of the properties? Does the unordered nature of these properties make it impossible to tell them apart?
In my case I'm thinking specifically of a situation where a function needs to accept a series of name-value pairs, but the names of the properties may change.
My thoughts on how to do this so far is to pass the names of the properties to the function along with the data, but this feels like a hack. I would prefer to do this with introspection if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以像这样循环遍历键:
这会记录“名称”和“值”。
如果您有更复杂的对象类型(不仅仅是像原始问题中那样的普通散列对象),您将只想循环属于对象本身的键,而不是对象的 原型:
正如您所指出的,不保证密钥按任何特定顺序排列。 请注意这与以下示例有何不同:
此示例循环遍历值,因此它将记录
Property Name
和0
。 注意:每个
语法大多仅在 Firefox 中受支持,而在其他浏览器中则不受支持。如果您的目标浏览器支持 ES5,或者您的网站包含
es5-shim.js
(推荐),您还可以使用Object.keys
:并使用
Array.prototype.forEach
:You can loop through keys like this:
This logs "Name" and "Value".
If you have a more complex object type (not just a plain hash-like object, as in the original question), you'll want to only loop through keys that belong to the object itself, as opposed to keys on the object's prototype:
As you noted, keys are not guaranteed to be in any particular order. Note how this differs from the following:
This example loops through values, so it would log
Property Name
and0
. N.B.: Thefor each
syntax is mostly only supported in Firefox, but not in other browsers.If your target browsers support ES5, or your site includes
es5-shim.js
(recommended), you can also useObject.keys
:and loop with
Array.prototype.forEach
:旧版本的 JavaScript (< ES5) 需要使用
for..in
循环:ES5 引入了 Object.keys 和 Array#forEach 这使得这变得更容易一些:
ES2017< /a> 引入
Object.values
和Object.entries
。Old versions of JavaScript (< ES5) require using a
for..in
loop:ES5 introduces Object.keys and Array#forEach which makes this a little easier:
ES2017 introduces
Object.values
andObject.entries
.您经常需要检查对象实例的特定属性,
没有它的所有共享原型方法和属性:
You often will want to examine the particular properties of an instance of an object,
without all of it's shared prototype methods and properties:
这将获取新对象中的所有属性及其值(继承的或拥有的,可枚举的或不可枚举的)。 原始对象未受影响。 现在可以使用以下方式遍历新对象
This will get all properties and their values (inherited or own, enumerable or not) in a new object. original object is untouched. Now new object can be traversed using
您可以使用 Object.keys(),“它返回一个给定对象自己的可枚举属性名称的数组,其顺序与我们使用普通循环得到的顺序相同。”
您可以使用任何对象来代替
stats
:You can use Object.keys(), "which returns an array of a given object's own enumerable property names, in the same order as we get with a normal loop."
You can use any object in place of
stats
: