0

I have a series of variables that I would like to pass into an object and I need the left side key to be pulled from a dynamic variable. How would I go about doing this?

Here's an example:

var characteristic = 'color';
var value = 'green';

// Desired JSON output
var object = {
  color: 'green'
}
Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111
user13286
  • 2,822
  • 8
  • 40
  • 83

1 Answers1

0

like so:

var characteristic = 'color';
var value = 'green';

var object = {
  [characteristic]: value
}

console.log(object);
MajiD
  • 2,191
  • 1
  • 22
  • 30