The full path of the json object

I am trying to smooth an object where the keys will be the full path to the node sheet. I can recursively identify which are the nodes of the sheet, but get stuck trying to build the whole path.

Input Example:

  {
   one: 1,
   two: {
     three: 3
   },
   four: {
     five: 5,
     six: {
       seven: 7
     },
     eight: 8
   },
   nine: 9
 }

Output:

  {
   one: 1,
   'two.three': 3,
   'four.five': 5,
   'four.six.seven': 7,
   'four.eight': 8,
   nine: 9
 }
+6
source share
6 answers

You can use a recursive approach and collect object keys. This sentence is also looking for arrays.

function getFlatObject(object) { function iter(o, p) { if (Array.isArray(o) ){ o.forEach(function (a, i) { iter(a, p.concat(i)); }); return; } if (o !== null && typeof o === 'object') { Object.keys(o).forEach(function (k) { iter(o[k], p.concat(k)); }); return; } path[p.join('.')] = o; } var path = {}; iter(object, []); return path; } var obj = { one: 1, two: { three: 3 }, four: { five: 5, six: { seven: 7 }, eight: 8 }, nine: 9 }, path = getFlatObject(obj); console.log(path); 
+9
source

 var obj = { one: 1, two: { three: 3 }, four: { five: 5, six: { seven: 7 }, eight: 8 }, nine: 9 }; function flatten(obj) { var flatObj = {} function makeFlat(obj, path) { var keys = Object.keys(obj); if (keys.length) { keys.forEach(function (key) { makeFlat(obj[key], (path ? path + "." : path) + key); }) } else { flatObj[path] = obj; } } makeFlat(obj, ""); return flatObj; } console.log(flatten(obj)); 
+1
source

Using the latest JS features, such as distributing objects and Object.entries , should be fairly simple:

 function flatObj(obj, path = []) { let output = {}; Object.entries(obj).forEach(([ key, value ]) => { const nextPath = [ ...path, key ]; if (typeof value !== 'object') { output[nextPath.join('.')] = value; return; } output = { ...output, ...flatObj(value, nextPath) }; }); } 

Note that this code is probably not the most optimal, as it copies an object every time we want to merge it. Consider it more as the essence of what it would look, rather than a complete and final decision.

0
source

Partial Solution: Give the input as the full path to the function and it will give you the corresponding output

 var obj = { one: 1, two: { three: 3 }, four: { five: 5, six: { seven: 7 }, eight: 8 }, nine: 9 }; function deepFind(obj, path) { var paths = path.split('.') , current = obj , i; for (i = 0; i < paths.length; ++i) { if (current[paths[i]] == undefined) { return undefined; } else { current = current[paths[i]]; } } return current; } console.log(deepFind(obj, 'four.six.seven')) 
0
source

An unusual approach, internally uses recursion.

 var x = { one:1,two:{three:3},four:{five: 5,six:{seven:7},eight:8},nine:9}; var res = {}; var constructResultCurry = function(src){ return constructResult(res,src); } function constructResult(target, src) { if(!src) return; target[src.key] = src.val; } function buildPath(key, obj, overAllKey) { overAllKey += (overAllKey ? "." : "") + key; if(typeof obj[key] != "object") return { key : overAllKey, val : obj[key] }; Object.keys(obj[key]).forEach(function(keyInner) { constructResultCurry(buildPath(keyInner, obj[key], overAllKey)); }); } Object.keys(x).forEach(function(k){ constructResultCurry(buildPath(k, x, "")); }); console.log(res); 
0
source

You can simply do the following:

 var obj = {one: 1, two: {three: 3}, four: {five: 5, six: {seven: 7}, eight: 8}, nine: 9}, flatObj = (o,p="") => { return Object.keys(o) .map(k => o[k] === null || typeof o[k] !== "object" ? {[p + (p ? ".":"") + k]:o[k]} : flatObj(o[k],p + (p ? ".":"") + k)) .reduce((p,c) => Object.assign(p,c)); }; console.log(flatObj(obj)); 
0
source

All Articles