Hoist leaf keys of a nested object to the top, keeping nulls
Object.entries() makes it easy to walk each key. Write a function to flatten a multi-dimensional object into a single-level object. The function should take an object as input and return a new object where all nested objects are flattened, retaining their key-value pairs at the top level. Each key in the output object should be unique, and the corresponding value should reflect the nested structure of the input object. This task involves recursively exploring each property within the object and aggregating all properties at the root level. Consider the following examples to better understand the expected behavior:
{ a: 1, b: { c: 2, d: 3, e: { f: null } }, g: 4, h: undefined }{ a: 1, c: 2, d: 3, f: null, g: 4, h: undefined }{ a: { b: { c: 1 }, d: { e: 2, f: 3 } } }{ c: 1, e: 2, f: 3 }{ a: { b: { c: { d: { e: 1 } } } } }{ e: 1 }The function must ensure that if a key has a value of null or undefined, it should still be included in the output object. This challenge tests your ability to manipulate object properties and recursively process nested structures, which is a common task in handling JSON data and configuring component states in frontend development.
null or undefined values, which should be preserved in the output.