I want to transform such object from:foo = {    42: 'foo',     'a.b.c[0].42': 'bar',    'a.b.c[0].43': 'zet',    'a.d.c[0].42': 'baz'}To:bar = {    42: 'foo',    'a.b.c[0].42': 'bar',    'a.b.c[0].43': 'zet',    'a.d.c[0].42': 'baz',    a: {        b: {            c: [{                42: 'bar', 43: 'zet'            }]        },        d: {            c: [{                42: 'baz'            }]        }    }}I have written implementation, but it looks ugly.Do anybody know how to improve the function below?We use lodash in my project so that can help with basic operations. function convertToTree(rowObjectData) {  function it(object) {      return _.reduce(object, function (finalObjectData, value, key) {          var pathParts = key.match(/(.+?)\.(.+)/);          if (pathParts) {              delete finalObjectData[key];              var deepObject = {},                  containerObject = {},                  arrayMatch = pathParts[1].match(/(.*)\[(.)]$/);              deepObject[pathParts[2]] = value;              if (arrayMatch) {                  containerObject[arrayMatch[1]] = [];                  containerObject[arrayMatch[1]][arrayMatch[2]] = it(deepObject);              } else {                  containerObject[pathParts[1]] = it(deepObject);              }              finalObjectData = _.defaultsDeep(finalObjectData, containerObject);          }          return finalObjectData;      }, object);  }  return _.assign(_.cloneDeep(rowObjectData), it(_.cloneDeep(rowObjectData)));}        var object = {    42: 'foo',     'a.b.c[0].42': 'bar',    'a.b.c[0].43': 'zet',    'a.d.c[0].42': 'baz'};        console.log(convertToTree(object));<script src=https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js></script>
Convert object with 'long.property.name' property to object tree
javascript;underscore.js;lodash.js