Home:ALL Converter>Create path from javascript object property

Create path from javascript object property

Ask Time:2017-06-07T00:51:34         Author:user2950509

Json Formatter

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

Author:user2950509,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/44395675/create-path-from-javascript-object-property
Nick is tired :

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:\n\n\r\n\r\nfunction getPath(obj, key) {\r\n paths = []\r\n\r\n function getPaths(obj, path) {\r\n if (obj instanceof Object && !(obj instanceof Array)) {\r\n for (var k in obj){\r\n paths.push(path + \".\" + k)\r\n getPaths(obj[k], path + \".\" + k)\r\n }\r\n }\r\n }\r\n\r\n getPaths(obj, \"\")\r\n return paths.map(function(p) {\r\n return p.slice(p.lastIndexOf(\".\") + 1) == key ? p.slice(1) : ''\r\n }).sort(function(a, b) {return b.split(\".\").length - a.split(\".\").length;})[0];\r\n}\r\n\r\nvar obj = { a:{ b:\"value\", c:{ d:\"value2\"}}};\r\nconsole.log(getPath(obj, \"b\"))\r\nconsole.log(getPath(obj, \"c\"))\r\nconsole.log(getPath(obj, \"d\"))\r\n\r\nvar obj = { d:{ d:\"value\", d:{ d:\"value2\"}}};\r\nconsole.log(getPath(obj, \"d\"))",
2017-06-06T17:33:06
Bibzer :

You can iterate over the object using a recursive function, and save the path like :\n\n\r\n\r\nfunction getPath(object, keyValue, path) {\r\n for (var k in object) {\r\n if (k === keyValue) {\r\n return path + '.' + k;\r\n }\r\n else if (typeof object[k] === 'object') {\r\n return getPath(object[k], keyValue, path !== '' ? path + '.' + k : k);\r\n }\r\n }\r\n}\r\n\r\nvar obj = {\r\n a:{\r\n b:\"value\",\r\n c:{\r\n d:\"value2\"\r\n }\r\n }\r\n};\r\n\r\nvar path = getPath(obj, 'd', '');\r\nconsole.log(path);",
2017-06-06T17:01:21
Nina Scholz :

You could use an iterative and recursive approach.\n\n\r\n\r\nfunction getPath(object, key) {\r\n\r\n function iter(o, p) {\r\n if (typeof o === 'object') {\r\n return Object.keys(o).some(function (k) {\r\n return iter(o[k], p.concat(k));\r\n });\r\n }\r\n if (p[p.length - 1] === key) {\r\n path = p;\r\n return true;\r\n }\r\n }\r\n\r\n var path = [];\r\n iter(object, []);\r\n return path.join('.');\r\n}\r\n\r\nconsole.log(getPath({ d: { d: { d: { d: 'val' } } } }, 'd'));\r\nconsole.log(getPath({ a: { b: 'value', c: { d: 'value2' } } }, 'd'));\r\n.as-console-wrapper { max-height: 100% !important; top: 0; }",
2017-06-06T17:31:52
yy