javascript object literal dynamic key SyntaxError [duplicate]

  • A+
Category:Languages

This question already has an answer here:

What is wrong with this expression?

['a', 'b'].map((x) => {[x]:x}) 

I'm getting this error:

Uncaught SyntaxError: Unexpected token : 

 


You need to wrap your {} in (), or it will be interpreted as the body of a function:

['a', 'b'].map((x) => ({[x]: x })) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: