const flatObj = obj =>
Object.entries(obj).reduce((acc, [ k, v ]) => (
v instanceof Object && !Array.isArray(v)
? Object.assign(acc, flatObj(v))
: acc[k] = v,
acc
), {});
const obj = { a: 100, b: 152, c: { y: 133, x: { m: 20} } }
const flatObject = (current) => {
return Object.keys(current).reduce((acc, cur) => {
if (typeof(current[cur]) !== 'object') {
acc[cur] = current[cur]
} else {
acc = {...acc, ...flatObject(current[cur]) }
}
return acc
}, {})
}
console.log(flatObject(obj))