Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const _ = require('lodash');
- const flattenObj = obj => _.reduce(
- obj,
- (accObj, value, key) => {
- if (_.isObject(value)) {
- _.forEach(
- flattenObj(value),
- ($value, $key) => {
- accObj[`${key}.${$key}`] = $value;
- },
- );
- } else {
- accObj[key] = value;
- }
- return accObj;
- },
- {},
- );
- console.assert(
- flattenObj({
- a: {
- b: 1,
- d: {
- l: 3,
- k: 4,
- },
- },
- c: 3,
- }) ===
- {'a.b': 1, 'a.d.l': 3, 'a.d.k': 4, c: 3},
- );
Add Comment
Please, Sign In to add comment