Some of the incoming objects I handle in my projects have unknown structures; properties I cannot depend on. I decided to try to build a function to help.
function getObjKeys(obj) {
var keys = [];
var regex = /"([^:,{}]+)":/g;
var str = JSON.stringify(obj);
var m;
while (m = regex.exec(str)) {
keys.push(m[1]);
}
return keys;
}
My question is, is there a better way of getting all the properties of an object with unknown structure? A mini-question also is; could the regex be improved? This is assuming the object is one level deep. I figure, after getting the keys, one could iterate through them to find other objects and therefore other keys.