Function: setting topic name with a string

#1

I am trying to assign the topic name using a string with the function node, however it does not work as expected.

var n1 = "var1"; // string will actually come from an incoming msg
dataset = {payload : {n1 : 12 , "var2" : 34}};
msg_o = dataset;
return msg_o;

This results in a payload of
{
n1 : 12,
var2 : 34
}
Instead of:
{
var1 : 12,
var2 : 34
}
Is this possible?

0 Likes

#2

try:

dataset = {payload : {[n1] : 12 , "var2" : 34}};
1 Like

#3

Here's the rules:
https://www.w3schools.com/js/js_object_properties.asp

0 Likes

#4

Thanks, this works great.

0 Likes