Created
October 23, 2019 14:42
-
-
Save bramus/6d1c41c652016551d577ddbc7d0f9703 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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