JavaScript Get json values for each level using javascript json object each,json object each jquery,json object each

I'm trying to use a recursive function to get the last key value form a simple json using javascript

I have this json:

{
  'a': {
    'b': {
      'c': 12,
      'd': 'Hello World'
    },
    'e': [1,2,3]
  }
}

And my expected result is:

{
  'a/b/c': 12,
  'a/b/d': 'Hello World',
  'a/e': [1,2,3]
}

I'm trying 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;
} 

But for some reason, it returns me:

a/b/c/d/e/0/1/, I'm not sure why it is adding those numbers there. Someone has an idea about how I can do it?

Answer:1

I think you may be making it more complicated than necessary. You can test for an array with Array.isArray and an non-object (typeof !== 'object) and just return the path and value. Otherwise recurse for each entry. reduce() is good for that. Passing the current path as an argument to the recursive function is convenient too:

let obj = {'a': {'b': {'c': 12,'d': 'Hello World'},'e': [1,2,3]}}

function getValues(obj, path = []){
    return (Array.isArray(obj) || typeof obj !== 'object')
    ? {[path.join('/')]: obj}
    : Object.entries(obj).reduce((acc, [key, val]) => 
        Object.assign(acc, getValues(val, path.concat(key)) )
    , {})
    }
    

console.log(getValues(obj))
Answer:2

I have looked through several threads/resources and can't seem to get working tooltips. Here is the code: nv.addGraph(function() { self.chart = nv.models.multiBarChart() ....

I have looked through several threads/resources and can't seem to get working tooltips. Here is the code: nv.addGraph(function() { self.chart = nv.models.multiBarChart() ....

Here is the Javascript: var bubbles = $('.history .bubble'); bubbles[bubbles.length-1].after('<div class="bubble me">' + text + '</div>'); Here is the HTML: Any idea why anything I ...

Here is the Javascript: var bubbles = $('.history .bubble'); bubbles[bubbles.length-1].after('<div class="bubble me">' + text + '</div>'); Here is the HTML: Any idea why anything I ...

I'm working on a website about donation using Gatsby v2 (Reactjs) and I need an example like open modal window on button donation click using Gatsby or ReactJS. I search on internet and I got nothing. ...

I'm working on a website about donation using Gatsby v2 (Reactjs) and I need an example like open modal window on button donation click using Gatsby or ReactJS. I search on internet and I got nothing. ...

i want to create a webpage like http://seller.flipkart.com . I want to scroll my page first from up to down and then left to right using scrolling button only and background drawing. Can you tell me ...

i want to create a webpage like http://seller.flipkart.com . I want to scroll my page first from up to down and then left to right using scrolling button only and background drawing. Can you tell me ...