Function: setting topic name with a string

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?

try:

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

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

Thanks, this works great.