admin管理员组文章数量:1318156
I have a simple javascript object with several unknown properties containing a value. The problem is that i don't really know the name of the field since it is variable. How can I access this unknown property?
For better understanding I have a simple object like following:
var a = { cat : "meow", dog : "woof"};
I need to read the name and the value of the different properties. I was thinking about something like the following: a.getField(0).name
.
I have a simple javascript object with several unknown properties containing a value. The problem is that i don't really know the name of the field since it is variable. How can I access this unknown property?
For better understanding I have a simple object like following:
var a = { cat : "meow", dog : "woof"};
I need to read the name and the value of the different properties. I was thinking about something like the following: a.getField(0).name
.
2 Answers
Reset to default 6You can do somethine like this:
for (var member in a) {
alert('Name: ' + member);
alert('Value: ' + a[member]);
}
for more info about reflection in JS see here:
http://lpetr/blog/archives/reflection-in-javascript
You could access the properties by name:
for (var key in a) {
var value = a[key];
}
Demo.
本文标签: javascriptread name of unknown propertiesStack Overflow
版权声明:本文标题:javascript - read name of unknown properties - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742040764a2417539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论