UNPKG

@e-group/utils

Version:

eGroup team utils that share across projects.

28 lines (23 loc) 664 B
export default function flatten(obj) { const result = {}; function traverseAndFlatten(currentNode, flattenedKey) { Object.keys(currentNode).forEach(key => { if (Object.prototype.hasOwnProperty.call(currentNode, key)) { let newKey; if (flattenedKey === undefined) { newKey = key; } else { newKey = "".concat(flattenedKey, ".").concat(key); } const value = currentNode[key]; if (typeof value === 'object') { traverseAndFlatten(value, newKey); } else { result[newKey] = value; } } }); } traverseAndFlatten(obj); return result; }