I have two variables: currValue and keys, which is a string of keys separated by .
like this: global.fontsize.bodyscale
. I want to create a nested json structure from this string. The json structure should like:
{
global: {
fontsize: {
bodyscale: {
value: currValue
}
}
}
}
How can I do this?
3
Answers
You can use
reduceRight
to repeatedly wrap the initial object with parent objects, based on the keys.You can use following code for this