I have a JSON object which comes back like this from a JavaScript API call:
{
"myArray": [
{
"version": 5,
"permissionMask": 1
},
{
"version": 126,
"permissionMask": 1
}
]
}
How can I access the name of the array (i.e myArray
) in JavaScript. I need to use the name of the array to determine the flow later on.
Use getOwnPropertyNames
to get a list of the properties of the object in array form.
Example:
var myObj = {
"myArray": [
{
"version": 5,
"permissionMask": 1
},
{
"version": 126,
"permissionMask": 1
}
]
},
names = Object.getOwnPropertyNames(myObj);
alert(names[0]); // alerts "myArray"
Note: If the object can have more than one property, like myArray
, myInt
, and myOtherArray
, then you will need to loop over the results of getOwnPropertyNames
. You would also need to do type-testing, as in if(names[0] instanceof Array) {...}
to check the property type. Based on your example in your question, I have not fleshed all of that out here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With