Add to array in an object

I've got an object msg.payload[0] and I now want to add another element to the array i.e I want to create msg.payload[1] as well

I can't get how to do this into my head

I'm doing this in Blockly but if someone could show me what to do in JavaScript I'll hopefully be able to transcode it :slight_smile:

In javascript you could Push an element.

msg.payload.push({"prop":1})

oh dear - don't think I've got that in Blockly :frowning:

I'm thinking that maybe I get the [0] value - save it temp - then delete obj - recreate it with 2 elements and repopulate [0]

Or

let index = msg.payload.count
msg.payload[index] = {"prop":1}

I'll try that
Unfortunately got bigger problems - my Blockly code has just vanished on me! :frowning:


But flows.json.backup has saved me :slight_smile:

Worked out what to do
Just create an empty list
then add first Object to it
then add second Object to it
then set msg.payload to the list

Dead simple :slight_smile:

PS Blocky has a insert at end of list block which is converted to a push :slight_smile:
image

That is what push does :smile:

1 Like

I'ts been 6 months since I last touched JS :slight_smile: