Skip to content Skip to sidebar Skip to footer

Get The "path" Of A Json Object In Javascript

I am trying to get the 'path' of an AngularJS scope variable and not having much luck. I want to eventually pass that 'path' to be used as the ng-model of some dynamic forms that a

Solution 1:

I think your error is at :

elseif (value_type === Object) {

      var result = get_path(value, target, path + "." + key);
      if (result) {
        return result;
      }
    }

you have added "." + key. just remove it become like below:

elseif (value_type === Object) {

      var result = get_path(value, target, path  );
      if (result) {
        return result;
      }
    }

Solution 2:

@min-hong-tan solved the problem, and should be the accepted answer. But for completeness sake, I added the following lines:

if (value === target) {
  return path + "." + key;
}

after each if block just in case I was trying to match an entire Array (as with my_data.children) or an entire Object that is not part of an Array.

Post a Comment for "Get The "path" Of A Json Object In Javascript"