JavaScript Javascript - Find path to object reference in nested object javascript find path in tree,javascript find path,javascript find

How can i recursively search a nested object to find the PATH to an object reference I'm providing?

My original object looks like:

a = {
 b: [
  { children: [...more objects] },
  { children: [] }
  etc..
 ],
 c: [
  { children: [...more objects] },
  { children: [] }
  etc..
 ]
}

I would like to call a function findDeepAndStorePath(a, obj) which would find the object reference and store the path to it in an array of indexes such as: ['b', 0, 1, 2].

Answer:1

function findPath(a, obj) {
    for(var key in obj) {                                         // for each key in the object obj
        if(obj.hasOwnProperty(key)) {                             // if it's an owned key
            if(a === obj[key]) return key;                        // if the item beign searched is at this key then return this key as the path
            else if(obj[key] && typeof obj[key] === "object") {   // otherwise if the item at this key is also an object
                var path = findPath(a, obj[key]);                 // search for the item a in that object
                if(path) return key + "." + path;                 // if found then the path is this key followed by the result of the search
            }
        }
    }
}

var obj = {
  "a": [1, 2, {"o": 5}, 7],
  "b": [0, [{"bb": [0, "str"]}]]
};

console.log(findPath(5, obj));
console.log(findPath("str", obj).split("."));                     // if you want to get the path as an array you can simply split the result of findPath
Answer:2

I've a form in which i'm displaying values from my DB through WebApi. One of the control will be having array of values. i've a scenario where user can edit and save it(PUT function). Controller : ...

I've a form in which i'm displaying values from my DB through WebApi. One of the control will be having array of values. i've a scenario where user can edit and save it(PUT function). Controller : ...

I found the following code in Magento 2 HTML: <div id="cart-totals" class="cart-totals" data-bind="scope:'block-totals'"> Magento 2 uses Knockout on frontend but I cannot found in Knockout ...

I found the following code in Magento 2 HTML: <div id="cart-totals" class="cart-totals" data-bind="scope:'block-totals'"> Magento 2 uses Knockout on frontend but I cannot found in Knockout ...

I have the following Vue HTML. I know this can be done with other modules but I prefer not to use unless it is needed. I have a select in a form the client fills out that has a select for the opening ...

I have the following Vue HTML. I know this can be done with other modules but I prefer not to use unless it is needed. I have a select in a form the client fills out that has a select for the opening ...

I would like to get a knob animated right after my ajax call is done. I get a number, which the knob should progress to without a refresh. So what i want is: Ajax request is succesfull -> sends number ...

I would like to get a knob animated right after my ajax call is done. I get a number, which the knob should progress to without a refresh. So what i want is: Ajax request is succesfull -> sends number ...