Skip to content

Instantly share code, notes, and snippets.

Created January 26, 2020 09:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
get, or set value nested objects.
// ref:
export function getValue<T>(obj: T, path: string): T {
if (!path) return obj;
const properties = path.split(".");
return getValue(obj[properties.shift()], properties.join("."));
// ref:
export function setValue<T>(obj: T, path: string, value: Pick<T, keyof T>): T {
const pList = path.split(".");
const key = pList.pop();
const pointer = pList.reduce((accumulator, currentValue) => {
if (accumulator[currentValue] === undefined) accumulator[currentValue] = {};
return accumulator[currentValue];
}, obj);
pointer[key] = value;
return obj;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment