Skip to content

Instantly share code, notes, and snippets.

@EvgenyArtemov
Created November 24, 2021 10:45
Show Gist options
  • Save EvgenyArtemov/4d760f738a24b4cd950b64e031bf0df0 to your computer and use it in GitHub Desktop.
Save EvgenyArtemov/4d760f738a24b4cd950b64e031bf0df0 to your computer and use it in GitHub Desktop.
const putAt = (path, obj, value) => {
const ObjCopy = {...obj};
const arrPath = path.split('.').reverse();
const copy = arrPath.reduce((acc, name, i) => {
const temp = {};
if(i === 0) {
temp[name] = value;
} else {
temp[name] = {...acc};
}
return {...temp};
}, ObjCopy)
return {...ObjCopy, ...copy};
}
console.log('putted:', putAt('a.b.c', {d: ''}, 'good!'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment