How to dynamically create the object?

Good day! Such situation: there is such interface:
interface IParams {
 value?: number;
 current?: number;
}

interface IObj {
 params?: IParams;
 age?: number;
 animal?: string;
}

You need to write a function that creates an object with only the key that came. That is, if the key value then the object should look like this:
{ 
 params: { 
 value: 5
}
}

I tried this implementation, but nesting more than 2 it doesn't work.

const paths = {
 value: ['params','numberic', 'value'],
 current ['params', 'current'],
 age: null,
 animal: null
}

const keysArray = Object.keys(paths);
const obj: IObj = {}
const toEnter = { value: 5}

const tail = ([, ...t]: string[]) => t;
const head = ([h]: string[]) => h;

const make = (path: string, value: any, intermediat: object) => {
 if (path.length === 1) {
 return intermediat[path[0]] = value
 } else {
 obj[head(path)] = {}
 make(tail(path), value, obj[head(path)]);
}
}

for (const keyToInput in toEnter) { 
 if (keysArray.indexOf(keyToInput) > -1) { 

 if (paths[keyToInput] !== null) {

 make(paths[keyToInput], toEnter[keyToInput], {});

 } else {
 obj[keyToInput] = toEnter[keyToInput]
}

}
}
April 19th 20 at 12:04
0 answer

Find more questions by tags TypeScriptJavaScript