Unflatten Object

Convert a flat object with dot notation keys to a nested object.

Code

General
const unflatten = (o) => Object.keys(o).reduce((acc, k) => {
  k.split('.').reduce((obj, key, i, arr) =>
    obj[key] = i === arr.length - 1 ? o[k] : obj[key] || {}, acc);
  return acc;
}, {});
return unflatten(obj);

Parameters

Flat object with dot notation

Browser·fetch() may be limited by CORS

More JavaScript Snippets