This commit is contained in:
Sparkadelic177
2020-08-07 10:16:26 -04:00
parent dedc12f269
commit 089779edf2

View File

@ -12,12 +12,12 @@ Otherwise, it adds the appropriate prefixed key-value pair to the accumulator ob
You should always omit the second argument, `prefix`, unless you want every key to have a prefix.
```js
const flattenObject = (obj, prefix = '') =>
const flattenObject = (obj, prefix = '') =>
Object.keys(obj).reduce((acc, k) => {
const pre = prefix.length ? prefix + '.' : '';
if (typeof obj[k] === 'object' && obj[k] !== null && Object.keys(obj[k]).length > 0) Object.assign(acc, flattenObject(obj[k], pre + k));
else acc[pre + k] = obj[k];
return acc;
const pre = prefix.length ? prefix + '.' : '';
if (typeof obj[k] === 'object' && obj[k] !== null && Object.keys(obj[k]).length > 0) Object.assign(acc, flattenObject(obj[k], pre + k));
else acc[pre + k] = obj[k];
return acc;
}, {});
```