Skip to content

Instantly share code, notes, and snippets.

@eleazarbr
Created December 10, 2020 23:20
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 eleazarbr/7db6476c049b5d46a84729cde0135848 to your computer and use it in GitHub Desktop.
Save eleazarbr/7db6476c049b5d46a84729cde0135848 to your computer and use it in GitHub Desktop.
How do I create a dynamic key to be added to a JavaScript object variable

If you're able to use ES6 JavaScript features, you can use Computed Property Names to handle this very easily:

var key = 'DYNAMIC_KEY',
obj = {
  [key]: 'ES6!'
};

console.log(obj);
// > { 'DYNAMIC_KEY': 'ES6!' }

Source: https://stackoverflow.com/a/2462810/6456709

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment