Working with arrays

Possible Noob-question :slight_smile: - yes i have seached in the docu and have found not an answer.

I create a msg with:

var num = msg.payload;
msg.payload = [{ "Test": parseFloat(num) }];
return msg;

Now i want to replace the "test" with a variable.

var num = msg.payload;
var testindex = "Test"
msg.payload = [{ testindex: parseFloat(num) }];
return msg;

But this does not work.
It results in an array with "testindex: 1673371589863" - but not with the variable like "Test: 1673371589863" as expected.

Is there a reason why it needs to output an array with a single object ?

a dirty way:

const num = msg.payload;
const property = "test"
msg.payload = [{ [`${property}`]: parseFloat(num) }];
return msg;

Ahhh. Great! That works well.
Thanks for the quick reply! :slight_smile:

To the question: It is for the elimination of multiple function nodes to only one and to avoid typos :wink:

Just for info. The string template is not needed

msg.payload = [{ [property]: parseFloat(num) }];

Should be fine.

Yeah thats what i thought, it didnt work here (initially), must have made a typo.

Looks good, but does not work.
The result is "undefined: 1673376899329"

You have some sort of typo or a undefined variable property.
As working perfectly fine here

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