Skip to content

Instantly share code, notes, and snippets.

@joladev
Last active August 15, 2019 05:42
Show Gist options
  • Save joladev/691cd1341008d04153e4632510ede3ad to your computer and use it in GitHub Desktop.
Save joladev/691cd1341008d04153e4632510ede3ad to your computer and use it in GitHub Desktop.
export const setIn = (obj: Object, key: string, value: string): Object => {
const levels = key.split('.');
const innerMostKey = levels.pop();
const innerMost = { [innerMostKey]: value };
const m = levels.reverse().reduce(
(acc, curr) => ({
[curr]: acc,
}),
innerMost as Object
);
return Object.assign(obj, m);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment