Created
March 9, 2022 12:50
-
-
Save aliemir/eb815998f698abcb8ec1ec2227b4ba0c to your computer and use it in GitHub Desktop.
Javascript - Remove property from object without mutating the original one in one line
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
/* | |
* Using destructuring, we can remove a dynamic key from an object without `delete` keyword in one line. | |
*/ | |
const baseObj = { a: 1, b: 2, c: 3 }; | |
const removeKey = "b"; | |
const { [removeKey]: _remove, ...newObj } = baseObj; | |
console.log(baseObj); // { a: 1, b: 2, c: 3 } | |
console.log(newObj); // { a: 1, c: 3 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment