Skip to content

Instantly share code, notes, and snippets.

@aliemir
Created March 9, 2022 12:50
Show Gist options
  • Save aliemir/eb815998f698abcb8ec1ec2227b4ba0c to your computer and use it in GitHub Desktop.
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
/*
* 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