node.js - Create path from javascript object property - Stack Overflow

Let's say I've got the following javascript objectvar obj = {a:{b:"value",c:{d:&quo

Let's say I've got the following javascript object

var obj = {
            a:{
                b:"value",
                c:{
                    d:"value2"
                }
            }
        }

What function would, when input with the "d" object (for example, function getPath(obj, d)), output the "a.c.d" string? I've tried various things including object-path, but it doesn't seem to be designed for that

Let's say I've got the following javascript object

var obj = {
            a:{
                b:"value",
                c:{
                    d:"value2"
                }
            }
        }

What function would, when input with the "d" object (for example, function getPath(obj, d)), output the "a.c.d" string? I've tried various things including object-path, but it doesn't seem to be designed for that

Share Improve this question edited Jun 6, 2017 at 16:55 user2950509 asked Jun 6, 2017 at 16:51 user2950509user2950509 1,0572 gold badges15 silver badges39 bronze badges 7
  • 4 Can you elaborate on "input with the "d" object"? Please provide an example of such a function call. If you mean something like path(obj.a.c.d), then there is no way to get the desired oute (because all the function gets is the string value "value2", it doesn't, and cannot, know about obj). – Felix Kling Commented Jun 6, 2017 at 16:53
  • 2 What are you trying to achieve with that? What if another property had a child 'd'? What if a parent is a 'd'? – baao Commented Jun 6, 2017 at 16:53
  • 1 How would this react to obj = {d:{d:{d:{d:"val"}}}} when asked for d? does it return d, d.d, d.d.d or d.d.d.d? – Nick is tired Commented Jun 6, 2017 at 16:55
  • @NickA It should return d.d.d.d @Felix Kling Would it not be possible if you also input "obj"? – user2950509 Commented Jun 6, 2017 at 16:56
  • 1 Possible duplicate of Return path to value in JavaScript object – Felix Kling Commented Jun 6, 2017 at 17:01
 |  Show 2 more ments

4 Answers 4

Reset to default 3

You could use an iterative and recursive approach.

function getPath(object, key) {

    function iter(o, p) {
        if (typeof o === 'object') {
            return Object.keys(o).some(function (k) {
                return iter(o[k], p.concat(k));
            });
        }
        if (p[p.length - 1] === key) {
            path = p;
            return true;
        }
    }

    var path = [];
    iter(object, []);
    return path.join('.');
}

console.log(getPath({ d: { d: { d: { d: 'val' } } } }, 'd'));
console.log(getPath({ a: { b: 'value', c: { d: 'value2' } } }, 'd'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

I had a fair amount of fun making this one up, it generates a list of every possible path that can be made from the object and returns the longest one ending with the correct key, it returns an empty string of not found:

function getPath(obj, key) {
    paths = []

    function getPaths(obj, path) {
        if (obj instanceof Object && !(obj instanceof Array)) {
            for (var k in obj){
                paths.push(path + "." + k)
                getPaths(obj[k], path + "." + k)
            }
        }
    }

    getPaths(obj, "")
    return paths.map(function(p) {
        return p.slice(p.lastIndexOf(".") + 1) == key ? p.slice(1) : ''
    }).sort(function(a, b) {return b.split(".").length - a.split(".").length;})[0];
}

var obj = { a:{ b:"value", c:{ d:"value2"}}};
console.log(getPath(obj, "b"))
console.log(getPath(obj, "c"))
console.log(getPath(obj, "d"))

var obj = { d:{ d:"value", d:{ d:"value2"}}};
console.log(getPath(obj, "d"))

You can iterate over the object using a recursive function, and save the path like :

function getPath(object, keyValue, path) {
  for (var k in object) {
    if (k === keyValue) {
      return path + '.' + k;
    }
    else if (typeof object[k] === 'object') {
      return getPath(object[k], keyValue, path !== '' ? path + '.' + k : k);
    }
  }
}

var obj = {
    a:{
        b:"value",
        c:{
            d:"value2"
        }
    }
};

var path = getPath(obj, 'd', '');
console.log(path);

Here you go. The function you want to use is findLongestPath:

var obj = {
    a:{
        b:"value",
        c:{
            d:"value2"
        }
    }
};

function findAllPaths(obj, property, startString = '') {
  var pathStrings = [];
  Object.keys(obj).forEach(key => {
    if(typeof obj[key] === 'object') {
      pathStrings.push(...findAllPaths(obj[key], property, startString + key + '.'));
      return;
    }
    
    pathStrings.push(startString + key);
  });
  
  return pathStrings;
}

function findLongestPath(obj, property) {
  return findAllPaths(obj, property)
    .filter(str => str.split('').reverse().join('').slice(0, property.length) === property.split('').reverse().join(''))
    .reduce((longest, next) => longest.length >= next.length ? longest : next, '');
}

console.log(findLongestPath(obj, 'd'));

findAllPaths is a recursive function that iterates through the object and generates an array of all possible object paths. findLongestPath filters those strings to make sure the last property matches the property given, and then returns the longest of those.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745262963a4619294.html

相关推荐

  • node.js - Create path from javascript object property - Stack Overflow

    Let's say I've got the following javascript objectvar obj = {a:{b:"value",c:{d:&quo

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信