Here is my response code in jQuery:
var response = $.parseJSON(response);
for (var i = 0; i < response.groupIds.length; i++) {
console.log(response.groupIds[i], i);
}
Each response.groupIds[i] is of the form {"unknown name":"unknown value"}.
I wish to access both of these bits of data in javascript, how do I accomplish this when I don’t know in advance what e.g. unknown name is?
Use
Object.keysto retrieve a full list (array) of key names. A polyfill is available here.Your question’s response format contains only one key-value pair. The code can then be reduced to:
If you’re not interested in the list, use a
for-i-inloop withhasOwnProperty. The last method has to be used, to exclude properties which are inherit from the prototype.