Massimo Ivaldi
Massimo Ivaldi

Reputation: 61

How can I push a JS variable into json

Sorry JS is not my language :-(

I should build such a json value:

var input = { 'to': { '[email protected]' : 'Jhon Doe' }

I have read a ton of docs but i am stuck whith:

var userEmail = '[email protected]'
var input = { 'to': { userEmail : 'Jhon Doe' }}
console.log(input);
// result-> userEmail: "Jhon Doe"

so, how can push a JS variable into json ?

Upvotes: 1

Views: 54

Answers (1)

Kirill Bulygin
Kirill Bulygin

Reputation: 3826

var input = { 'to': { [userEmail] : 'Jhon Doe' }}

[userEmail] will turn to the value of userEmail.

Alternatively:

var input = { 'to': {} };
input[userEmail] = 'Jhon Doe';

Upvotes: 2

Related Questions