Add flattenObject, unflattenObject

Tagged and tested. Quite complex methods btw.
This commit is contained in:
Angelos Chalaris
2018-02-07 11:30:18 +02:00
parent 534820e6f3
commit 3425d1f6f8
8 changed files with 1023 additions and 893 deletions

View File

@ -0,0 +1,12 @@
const flattenObject = (obj, prefix = '') =>
Object.keys(obj).reduce((acc, k) => {
const pre = prefix.length ? (prefix + '.') : '';
if (typeof obj[k] === 'object')
Object.assign(
acc,
flattenObject(obj[k], pre + k)
);
else acc[pre + k] = obj[k];
return acc;
}, {});
module.exports = flattenObject;