Implement a squashObjectKeys function
Squashing keys refers to the process of transforming a nested objects into a single-level object by flattening out the keys of the object.
1. Basic implementation
Squash keys for an object and use . as seperator. For array values use index as keys.
The utility to generate new keys which are squashed can be implemented as follows:
2. With depth implementation
In above example, we squashed all keys for any depth of the object or array. In this case, we should only squash keys until a certain depth of the object.
As most of the implementation remains exactly same, I will highlight only the newer additions made on the utility.
3. With empty keys implementation
Since object keys are strings, there can be cases where some keys maybe be passed as empty spaces. In such cases, we simply need to ignore the key, instead of generating squashed keys with empty spaces in between like const key='a.b.c.''.d'
For this implementation we don’t really need to anything within the squashObjectKeys
function, we can simply alter the getSquashedKey
utility created as such:
Further Reading
I strongly encourage you to explore and tackle additional questions in my Recursion Questions for Frontend Interviews blog series.
By doing so, you can enhance your understanding and proficiency with recursion, preparing you to excel in your upcoming frontend interviews.
Wishing you best. Happy Interviewing 🫡.