JSFiddle - React, Tailwind, and code Playground

by chridam

JavaScript

function path(a) {
    var list = [];
  (function(o, r) {
    r = r || '';
    if (typeof o != 'object') {
      return true;
    }
    for (var c in o) {
      if (arguments.callee(o[c], r + "." + c)) {
        list.push(r.substring(1) + "." + c);
      }
    }
    return false;
  })(a);
  return list;
}


function flatten(obj) {

    var result = result || {},
        temp;

    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            if (Object.prototype.toString.call(obj[key]) == '[object Object]') {
                temp = flatten(obj[key]);
                for (subkey in temp) {
                    result[key + '.' + subkey] = temp[subkey];
                }
            }
            else result[key] = obj[key];
        }
    }

    return result;
}


var a = {
  great:{
    grand:{
      parent:{
        child:1
      },
      parent2:1
    }
  }
};

var obj = {
    param1 : {
        subParam1: 10,
        subParam2: 20
    },
    param2: 123
}

console.log(JSON.stringify(path(a)));
console.log(JSON.stringify(path(obj)));

console.log(JSON.stringify(flatten(a)));
console.log(JSON.stringify(flatten(obj)));