I want to remove the payload from the message into a new object and do a Object.assign with a new object to the payload. Then insert the new object into the msg. I have no issues adding msg.payload to a variable and performing the Object.assign to insert the new object. The problem is when that when I do a delete msg.payload the type, client and topic objects are deleted as well. Since the objects that precede the payload change I can't hard code that.
Is there any way to delete the payload while preserving the rest of the objects?
Yes I should have called them properties, however the entire msg is a object. I tested this by running a simple function that does
msg = delete msg.payload
return msg
when I do this the only output I get is a Boolean value of true. I assumed that the properties that are not in the payload would still be available. The idea is to
let band = "band: 40"
let Obj = msg.payload
let newObj = delete.msg.payload
Obj = Object.assign(band, Obj)
return Object.assign(newObj, Obj)
This would allow me to place the band property at the beginning of the msg.payload and then add to the newObj which would have the other properties.
Since the properties are not an object as you stated then I think this may be where I am going off track. When I did this I got the properties in the Obj variable but not the newObj properties.
So the question then is how can I strip out the properties that are not in the payload into an object?
This doesn't make sense. First of all, delete.msg.payload indicates you have an object called "delete". However delete is a keyword. If you want to do the action to delete something, make sure to have space afterwards! Further, with equals you indicate you want to save the output of the delete action into newObj. But a delete action doesn't return anything. It deletes the data. That is not something you can save in a new variable.
Also, you use Object.assign to try to merge a string with an object? That won't work. You can read more about it here: Object.assign() - JavaScript | MDN
I would recommend starting with simpler things and perhaps stay away from Object.assign as it is more advanced.
Can you explain what you have as input and what you would have as output? The more exact details on incoming object and structure of the output you want, the better feedback I can give.
I would recommend playing around with javascript first, for example here: JavaScript Playground
If you try this code, you will clearly see how delete works:
Allright, indent is a bit off and semi-colon usage is inconsistent, but at least it works so congrats! I'd still lay off Object.assign as I have yet to see a good place to use it and for this level of coding it will most likely just make things more complicated. It's hard for me to understand what's going on.
Why can't you simply do:
msg.payload.band = band; // assuming `band` is a variable that already exists in your scope
return msg;
?
I noticed you mentioned you wanted band property at the beginning of msg.payload. But if msg.payload is an object, javascript objects are really maps and more specifically unordered. So you can't order props inside an object. If you need ordered properties, you must use array (or possibly ordered maps, but that's more of an advanced topic). Further, you want to strip out the properties not in payload? Why not use delete msg.payload? I noticed you extract payload into variable called Obj, but then later add that payload back into msg? What's the point of stripping it out if you just gonna put it back in?
Here is where I prepend the band object to the beginning of the vari obj. The next step is to apend the var obj to msg where payload was deleted. This way the payload does not show up twice.
If I use msg.payload.band it works but it appends the object to the end of the payload. The goal was to prepend it to the payload. It's not as efficient as msg.payload.band but I wanted to use Object.assign to prepend it.
I could use array.unshift or array.push but then I have to convert it back to an object. I don't know if that actually less cpu that the Object.assign and once I went down that road I was determined to make it happen.
I will now go back and try array.unshift / array.push for fun.
At the end of the day it does not matter. At 1st I felt it was better represented as the 1st property. When I was struggling to get it to to work then it became a goal to make it happen. Node-RED was my 1st look into JavaScript and it is as much a learning experience as it its a means to an end.
Ok all of this is to attempt to modify the order of a javascript object. That is futile and you're working uphill from here. An object in javascript is essentially a map with string keys, effectively unordered in practice but as you discovered it retains insert order. It is not designed for what you want. If you really needed order, there are more advanced options: @js-sdsl/ordered-map - npm