Below is my object:
var obj = {
'test': {
'id1': 1,
'id2': 2,
'id3': 3
}, 'test2': {
'id4': 4,
'id5': {
'id123': 3,
'id456': 6
}
},
'test3': 45,
'test4': 55
}
I am trying to write a recursive function for getting nested properties in the format like.
test.id1, test2.id5.id123 etc...
But I am not getting properties which are nested as shown above.
Below is my function
function getKey(obj, key) {
var keyArr = [];
var newKey = key;
var data=newKey;
if (_.isObject(obj[key])) {
_.each(_.keys(obj[key]), function(singleKey) {
newKey = data + '.'+ getKey(obj[key], singleKey);
return newKey
});
}
return keyArr.length ? keyArr : newKey;
}
I am calling this function like var keys = _.keys(obj); _.each(keys, function(key) { getKey(obj, key); }
Can anyone help with this issue? Thanks