While trying to become more comfortable with recursive functions, I attempted to write a function that takes a dot-separated string and converts that into an object.
So, given the following string: user.first.name.robert this function should return the following object:
{
user: {
first: {
name: 'robert'
}
}
}
Here's my attempt:
function objectifyHelper(array, object) {
if (array.length === 2) {
const [key, value] = array;
object[key] = value;
return object;
} else {
object[array[0]] = objectifyHelper(array.slice(1), object);
return object;
}
}
function objectify(string) {
const tokens = string.split('.');
return objectifyHelper(tokens, {});
}
const str = 'user.first.name.robert';
const result = objectify(str);
This gets me the following result:
result <ref *1> {
name: 'robert',
first: [Circular * 1],
user: [Circular * 1]
}
What am I doing wrong?