I need help using a recursive function in JavaScript to retrieve the last key value pair from a simple JSON object.
Here is an example JSON object:
{
'a': {
'b': {
'c': 12,
'd': 'Hello World'
},
'e': [1,2,3]
}
}
The expected result should look like this:
{
'a/b/c': 12,
'a/b/d': 'Hello World',
'a/e': [1,2,3]
}
This is the function I am currently trying to work with:
function getDeepKeys(obj) {
var keys = [];
for (var key in obj) {
keys.push(key);
if (typeof obj[key] === "object") {
var subkeys = getDeepKeys(obj[key]);
keys = keys.concat(subkeys.map(function (subkey) {
return key + "/" + subkey;
}));
}
}
return keys;
}
However, the output I am getting includes unexpected numbers like a/b/c/d/e/0/1/
. Can anyone suggest a solution to fix this issue?