SamelK
SamelK

Reputation: 11

Javascript add a variable in this JSON

I have the following var x = "bob" and want to put var x value into "something"

var request = {
    "id":"2",
    "number":"12345",
    "params":[{
        "name":"bob",
        "message":"hi"
    }],
    "something": "${x}"
 }

I am new to JS and NodeJS and am stumped on how to easily do this.

Upvotes: 1

Views: 48

Answers (3)

hamidreza nikoonia
hamidreza nikoonia

Reputation: 2165

you must use (dot) to change a javascript Object

var x = 'bob';

request.something = x;

Upvotes: 0

brk
brk

Reputation: 50291

Access the key using dot(.) or square notation[]

var x = "bob"
var request = {
  "id": "2",
  "number": "12345",
  "params": [{
    "name": "bob",
    "message": "hi"
  }],
  "something": "${x}"
}
request.something = x;
console.log(request)

Upvotes: 0

Nick
Nick

Reputation: 16576

All you have to do is

request.something = x;

Upvotes: 1

Related Questions