JavaScript Object - Remap Properties (by Key)
<p>Can you think of a better way to <strong>remap</strong> this JavaScript object:</p>

<pre><code>{
    "id": 123,
    "child.id": 456,
    "child.child.id": 789
}
</code></pre>

<p>... into this JavaScript object:</p>

<pre><code>{
    "id": 123,
    "child": {
        "id": 456,
        "child": {
            "id": 789
        }
    }
}
</code></pre>

<p>There must be a shorter or faster solution. This is my attempt:</p>

<pre><code>var original = {
    "id": 123,
    "child.id": 456,
    "child.child.id": 789
};

var result = {};

Object.keys(original).forEach(function(key) {
    var node = result;
    var keys = key.match(/(\w+)/g);

    for (var i = 0; i &lt; keys.length; i++) {
        if (!node[keys[i]]) {
            node[keys[i]] = {};
        }
        if (i == keys.length - 1) {
            node[keys[i]] = original[key];
        } else {
            node = node[keys[i]];
        }
    }
});
</code></pre>
