I have encountered a unique scenario while using a specific library. Instead of returning an array, this library returns nested objects with the final leaf node containing the value. For instance:
red.walk.fast.true
is returned as {red: {walk: {fast: 'true'}}}
or
climbing.ascending
is returned as {climbing: 'ascending'}
The current format of nested objects serves my purpose, but I require a method to convert the final value into another nested object with a null value. Therefore:
{red: {walk: {fast: 'true'}}}
should become
{red: {walk: {fast: {true: null}}}}
This transformation should work for any deeply nested object. I am facing challenges implementing a recursive function using Ramda for this task. I tried the following approach:
const stringPropToObject = R.ifElse(
R.is(String),
R.assoc(R.__, null, {}),
mapAllStringPropsToObjs);
const mapAllStringPropsToObjs = R.map(stringPropToObject)
const updatedObject = mapAllStringPropsToObjs({ a: { b: { c: 'd' } } })
console.log(updatedObject);
mapAllStringPropsToObjs attempts to iterate over an object's properties and pass them to stringPropToObject. The function then evaluates the property being passed in.
- If the property is a string, it returns a new object with that string.
- If the property is an object, it should recursively call mapAllStringPropsToObjs until a string value is encountered.
However, I am encountering an error "Cannot access 'mapAllStringPropsToObjs' before initialization." I understand why this error is occurring due to the current order of declarations, but I am unsure how to reorder them to resolve the issue as both functions are interdependent.
Is there any advice on using Ramda or vanilla JS to convert:
{climbing: 'ascending'}
to {climbing: {ascending: null}}
and
{red: {walk: {fast: 'true'}}}
to
{red: {walk: {fast: {true: null}}}}
or any other nested value with arbitrary depth?
I appreciate any suggestions provided. Thank you.