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
  • 795,719
  • 175
  • 1,089
  • 1,143
user13286
  • 3,027
  • 9
  • 45
  • 100

1 Answers1

0

like so:

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

var object = {
  [characteristic]: value
}

console.log(object);
MajiD
  • 2,420
  • 1
  • 22
  • 32