0

how can I dynamically create a key, value of existing object JavaScript?

temp1 = {}  **//object** 

temp1.map(a=>{return{"key":a.books[0].book}}) **//works fine how can i set dynamic key** 

I want to set key like this  a.books[0].name : //

Now code turn on temp1.map(a=>{return{**a.books[0].name** : a.books[0].book}}) 
//Uncaught SyntaxError: Unexpected token '.' i got error like this 
Harish Verma
  • 548
  • 7
  • 17
  • 1
    `return {[a.books[0].name]: a.books[0].book}` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer – zerkms Oct 14 '19 at 21:56
  • @zerkms thanks so much you are saving my time thanks for share good reference. – Harish Verma Oct 14 '19 at 22:00

0 Answers0