Skip to content

Instantly share code, notes, and snippets.

@bramus
Created October 23, 2019 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bramus/6d1c41c652016551d577ddbc7d0f9703 to your computer and use it in GitHub Desktop.
Save bramus/6d1c41c652016551d577ddbc7d0f9703 to your computer and use it in GitHub Desktop.
const changeValueInObject = (object, key, value) => {
if (key.indexOf('.') !== -1) {
const keyParts = key.split('.');
const firstPartOfKey = keyParts.shift();
return {
...object,
[firstPartOfKey]: {
...(object[firstPartOfKey] ?? {}),
...changeValueInObject((object[firstPartOfKey] ?? {}), keyParts.join('.'), value),
},
};
}
return {
...object,
[key]: value,
};
}
export default changeValueInObject;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment