Convert object with 'long.property.name' property to object tree
If I had to take a stab at it I would write something like this:



function convertToTree(rowObjectData) {

  function isArrayKey(key) {
    return _.last(key) == ']';
  }
  
  function setValue(object, value, key) {
    var parts   = key.split(/[.[]/),
        last    = parts.length - 1;

    for (var i=0, current=object; 
         i &lt; parts.length; 
         i++, current = current[k]) {
      var p = parts[i],
          k = isArrayKey(p) ? parseInt(p) : p;
      if (i == last)
        current[k] = value;
      else if (current[k] == undefined)
        current[k] = isArrayKey( parts[i+1] ) ? [] : {}
    } 
    
    return object;
  } 
  
  return _.reduce(rowObjectData, setValue, _.cloneDeep(rowObjectData) );
}
        
var object = {
    42: 'foo', 
    'a.b.c[0].42':    'bar',
    'a.b.c[0].43':    'zet',
    'a.d.c[0].42':    'baz',
    'a.d.c[1]':       'zaphod',
    'a.d.c[2][0].49': 'beeblebrox',
};
        
console.log(convertToTree(object));
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"&gt;&lt;/script&gt;




Which also allows multiple sequential arrays (c[0][1].49 = 'foo' and doesn't require the last key to be an object (c[0] = 'foo')