Array.prototype.reduce()
to convert the flat path to a leaf node.String.prototype.split('.')
to split each key with a dot delimiter and Array.prototype.reduce()
to add objects against the keys.const unflattenObject = obj => Object.keys(obj).reduce((res, k) => { k.split('.').reduce( (acc, e, i, keys) => acc[e] || (acc[e] = isNaN(Number(keys[i + 1])) ? keys.length - 1 === i ? obj[k] : {} : []), res ); return res; }, {});
unflattenObject({ 'a.b.c': 1, d: 1 }); // { a: { b: { c: 1 } }, d: 1 }
unflattenObject({ 'a.b': 1, 'a.c': 2, d: 3 }); // { a: { b: 1, c: 2 }, d: 3 }
unflattenObject({ 'a.b.0': 8, d: 3 }); // { a: { b: [ 8 ] }, d: 3 }
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️