[Solved] Convert one level object to nested objects in javascript [closed]


A rough implementation can be

function convert(obj) {
    var res = {}, i, j, splits, ref, key;
    for (i in obj) {
        if (obj.hasOwnProperty(i)) {
            splits = i.split('.');
            ref = res;
            for (j = 0; j < splits.length; j++) {
                key = splits[j];
                if (j == splits.length - 1) {
                    ref[key] = obj[i];
                } else {
                    ref = ref[key] = ref[key] || {};
                }
            }
        };
    }
    return res;
}

Demo: Fiddle

1

solved Convert one level object to nested objects in javascript [closed]