0

I'm looking for a way to do the following in an immutable style, i.e., not needing to instantiate an empty object a first.

let a = {};
let append = '123'
let keyName = `myKey${append}`
a[keyName] = 'hello world';

// result
//a = {
//  myKey123: 'hello world'
//}
ABC
  • 1,387
  • 3
  • 17
  • 28
  • Why you dont want to instantiate it? Is it because you dont want it to rewrite it to `{}` if you already have keys? – Jeremy Rajan Feb 25 '16 at 06:11
  • 2
    Are you perhaps looking for [computed properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names)? `let a = { [keyName]: 'hello world' };` – Jonathan Lonowski Feb 25 '16 at 06:17
  • Thanks, computed properties is what I was looking for! I just didn't know the name of it. If you put it as an answer I'll accept it. – ABC Feb 25 '16 at 09:47

0 Answers0