Add flattenObject, unflattenObject
Tagged and tested. Quite complex methods btw.
This commit is contained in:
12
test/flattenObject/flattenObject.js
Normal file
12
test/flattenObject/flattenObject.js
Normal 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;
|
||||
Reference in New Issue
Block a user