First of all I am not sure whether it is actually possible in javascript, but still I felt it’s worth asking.
Ok, so what I am trying to do is get the names of the members of an array (or object as you might say) dynamically at runtime.
Let me explain. I have an object like this :
Results :-
member_name1 : value_1
member_name2 : value_2
member_name3 : value_3
Here, Result is the name of the object which has members like member_name1, member_name2, etc and theu have values like value_1 and value_2 respectively. What I am trying to do is get the name of the members like member_name1 this at runtime; not it’s value. I access the value by Results.member_name1 generally.
I hope I am able to portray the issue!
Below is a screenshot of the object :
https://i.stack.imgur.com/dzAgm.png
Thanks!
Assuming
objis your object, you can get the names of all its properties this way:However, be aware that this will also pick up any extensions that have been made to
objthrough theprototypeproperty of its class. If you want to exclude these and only get the properties defined onobjitself, you can filter them out withMore information on
for...inon MDN.