Modifying object variable name

Hi!
I am having some trouble with this simple task:
I have a msg.payload like this:
"response": "221321312"
And I want to be able to change it for "WhateverIWant":"221321312" .
I have tried with change and switch but I am lost, it would be sweet if you can help me.
Thanks in advance!

Do you mean that:

msg.payload = {'response':'221321312"}

In other words, the payload is an object?

If so:

msg.payload = { 'WhateverIWant': msg.payload.response }
2 Likes

Thanks! I always forget about objects that was my error.
Regards!

Is there a possibility to make the object property 'WhateverIWant' a string variable?

So that it can be changed dynamically during the running of the program?
For example for each loop adding another number to the variable as indicator?

string = 'WhateverIWant' + string(incrementing value)
msg.payload = { string: msg.payload.response }

msg.payload = {[string]: msg.payload.response};
or
msg.payload[string] = msg.payload.response;

1 Like

Thank alot, this fixed my problem!
Probably a noob mistake because I am new to javascript :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.