Use the variable as a key to the object



  • Something like:

    var key = "myKey";
    myArray.push( { key : value} );
    

    I know you can:

    var key = "myKey";
    var obj = {};
    obj[key] = value;
    myArray.push(obj);
    

    But it doesn't work, because it's when I set up the site that I have to turn to the key.



  • Ok, ES6 can be:

    var key = "myKey";
    myArray.push( { [key] : value} );
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2