function isObject(obj: any): boolean { return typeof obj === 'object' && obj !== null; } export function set(object: object, path: Array, value: any) { const p = [...path]; const lastKey = p.pop(); let nested: any = object; for (let i = 0; i < p.length; i += 1) { const key = p[i]; if (isObject(nested)) { nested = nested[key]; } else { break; } } // p.forEach((key) => { // if (isObject(nested)) { // nested = nested[key]; // } else { // return false; // } // }); if (isObject(nested) && lastKey !== undefined) { nested[lastKey] = value; return true; } return false; }