JavaScript
Dynamic property names

by Benny Neugebauer

const TABLE_NAME = 'the-simpsons';

const DEFINITION = (
  obj = {},
  obj['' + TABLE_NAME] = 'name, age',
  obj
);

const TABLE_NAME = 'the-simpsons';

const DEFINITION = {};
DEFINITION[TABLE_NAME] = 'name, age';

With ES6 there is another possibility! https://vote4code.com/5727270706610176/using-a-variable-key-in-an-object-literal

Benny Neugebauer

Sign in to comment!