This question has already been answered:

How to add a variable value to an object and not a variable name?
I get {"tempIndex":"hello"} but I would like {"1":"hello"} .

 var index = ["1","2"] var names = ["hello","dude"] var tempIndex = index[0] var tempName = names[0] var obj = {} obj.tempIndex = tempName document.write(JSON.stringify(obj)) 

Reported as a duplicate by Bogdan Shulga members , Community Spirit Oct 15 '16 at 8:44 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

     var index = ["1","2"] var names = ["hello","dude"] var tempIndex = index[0] var tempName = names[0] var obj = {} obj[tempIndex] = tempName; for(var item in obj){ document.write(item + " -- " + obj[item]) } 

    • Thank. Already found in similar issues) - Bogdan Shulga
    • Can you tell me how to get the name: value fields through foreach now? And then for(var element in obj) returns only the value) - Bogdan Shulga
    • one
      for (var item in obj) {var key = obj [item]} - C.Raf.T
    • Thank. I also found this option for (var k in obj){ if (obj.hasOwnProperty(k)) { document.write("Key is " + k + ", value is " + obj[k]); } } for (var k in obj){ if (obj.hasOwnProperty(k)) { document.write("Key is " + k + ", value is " + obj[k]); } } - Bogdan Shulga
    • @BogdanShulga already corrected the answer ...)) - C.Raf.T