Hi i make a client call to a hhtp get webservice using the http module
I pass options as a input to the function.Below is the code snippet
var headerKey="ddd"
options = {
host: 10.0.0.0,
port: 80,
path:/pppp,
headers: {
headerKey : 1
}
};
When i print options below is the output
options:-{"host":"10.0.0.0","port":80,"path":"/pppp","headers":{"headerKey":"1"}}
My issue is that the "headerkey" is not changing to ddd instead its passing the variable name as the parameter.(ie) I want the out put as below
{"host":"10.0.0.0","port":80,"path":"/pppp","headers":{"ddd":"1"}}
I am stuck here.Any help regarding this will be really helpful.
If you mean changing option's headerKey to ddd
var headerKey = 'ddd';
options = {...};
options.headers.headerKey = headerKey;
As far as I know, you can't rename a property in JS. You could add another property, and have it point to the same value as the previous:
options.headers[headerKey] = options.headers.headerKey;
delete options.headers.headerKey;
check out this working example
var headerKey="ddds"
var options = {
host: "10.0.0.0",
port: "80",
path:"/pppp",
headers: {}
};
options.headers[headerKey] = '1';
console.log(options);
console.log(options.headers);
To use a variable as a key in javascript you can use the array notation of the objects.