Setting multiple objects in payload


I need to set multiple objects in a payload before sending.

I need it to look like:

pool_api_code: ABC
temperature_scaler: 1

I have tried:

msg.payload = {"pool_api_code": "ABC" + "temperature_scaler": 1 }

but that does not work. Can anyone help me with the correct syntax?

I need to set multiple objects in a payload before sending.

Do you mean that you need to set multiple properties ?

There are multiple ways - the concept is:
{ property: value, property: value, ... }

some examples

msg.payload = { pool_api_code:"ABC", temperature_scaler: 1 }
return msg


const payload = {}
payload.pool_api_code = "ABC"
payload.temperature_scaler = 1

msg.payload = payload
return msg

or (this will create the property names automatically)

const pool_api_code = "ABC"
const temperature_scaler = 1
msg.payload = {pool_api_code, temperature_scaler}

I didn't know you could do that. Thanks for the education. Did that appear in a recent version of nodejs or have I just been asleep at the wheel?


I do this often, I think it's been around for some time, but can't find any reference to when it was a thing

Found - The computed property names feature was introduced in ECMAScript 2015(ES6)

Object.assign/create may be a better choice if it is uncertain if the object changes afterwards.
But this can also be very useful if you want to combine multiple objects into a single object, by using the spread operator:

const a = {a:true}
const b = {b:[],c:false}
const obj = {...a, ...b}   // {"a":true,"b":[],"c":false}

Same can be done with arrays as well (also to get unique values with [ Set(array)], but we are getting distracted ;))

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