Skip to main content
edited body
Source Link
alecxe
  • 17.3k
  • 8
  • 51
  • 93

I'm wondering how I can improve my getPath function (I found it on SO and adjusted it to my needs - biggest change was returning an array, used to be a string).
It

It takes an object and a value as inputs and returns an array of object keys for the first matching value they find, otherwise false.

I wrote that one comment as a reminder to myself because I spent too much time wondering why the function didn't work before figuring this out. :D

I'm wondering how I can improve my getPath function (I found it on SO and adjusted it to my needs - biggest change was returning an array, used to be a string).
It takes an object and a value as inputs and returns an array of object keys for the first matching value they find, otherwise false.

I wrote that one comment as a reminder to myself because I spent too much time wondering why the function didn't work before figuring this out. :D

I'm wondering how I can improve my getPath function (I found it on SO and adjusted it to my needs - biggest change was returning an array, used to be a string).

It takes an object and a value as inputs and returns an array of object keys for the first matching value they find, otherwise false.

I wrote that one comment as a reminder to myself because I spent too much time wondering why the function didn't work before figuring this out.

Source Link
Cold_Class
  • 115
  • 1
  • 7

Getting path array for given value in nested Object

I'm wondering how I can improve my getPath function (I found it on SO and adjusted it to my needs - biggest change was returning an array, used to be a string).
It takes an object and a value as inputs and returns an array of object keys for the first matching value they find, otherwise false.

My focus isn't primarily the speed.
Focus is: As short but at the same time as easy to understand as possible without having to write many comments.

I'm using JS before ECMAScript 6

var nestedObj = {
  "rrr": {
    "ddd": {
      "aa    bbbb": {
        1: 30009463
      },
      "cc    dddd.": {
        1: 30010338
      },
      "e     fff": {
        1: 30007744
      },
      "d-Pool": {
        1: 30018363,
        2: 30017133,
        3: 30013107
      },
      "e   e g": {
        1: 30011707,
        2: 30017137
      },
      "f   f.-f. (f)": {
        1: 30012329
      },
      "g": {
        1: 30011894
      }
    }
  }
};

var result = getPath(nestedObj, 30017137);
document.getElementById("result").innerHTML = result;

function getPath(obj, value, path) {
  if (typeof obj !== 'object') {
    return false;
  }
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      var t = path;
      var v = obj[key];
      //SLICE is super important, otherwise newPath will reference path
      var newPath = path ? path.slice() : [];
      newPath.push(key);
      if (v === value) {
        return newPath;
      } else if (typeof v !== 'object') {
        newPath = t;
      }
      var res = getPath(v, value, newPath);
      if (res) {
        return res;
      }
    }
  }
  return false;
}
<div id="result"></div>

I wrote that one comment as a reminder to myself because I spent too much time wondering why the function didn't work before figuring this out. :D