I expanded the body of the for loop and added some comments in an attempt to make things more explicit.
for (var i = 0, length = array.length; i < length; i++) { // Assign the current item in the array to a variable var current = array[i]; // If there is no property on the "tree" object corresponding to the value of // "current", set this property to a new object if (!tree[current]) { tree[current] = {}; } // Set the "tree" variable to the field in the "tree" object whose // name corresponds to "current". On the next loop iteration, "tree" will // refer to this "child" object, resulting in a tree-like object being // created as we iterate. tree = tree[current]; }
Donut source share