How can I set an Object property dynamically

Hi guys,
I am having some trouble setting a rule on NodeRED json object.
You can find below my code. So I have a dynamic sensor JSON data. Also, I have an object which is an object within an object. I want to set a rule on the latest value of that object since it is dynamic. Also, I tried using map method but that maps all the values to an array. Then I used includes method to set a rule on the array. But what I want is to set a rule only on the latest (newest value) and not the array. Could you please suggest I better method to solve the issue.
[ { "id": "2c32d62d18562bec", "type": "function", "z": "5e7b3b3f21c31bd2", "name": "Location", "func": "msg.payload = \n{\n \"subject\": {\n \"personId\": 123,\n \"firstName\": \"Patrick\", \n \"lastName\": \"2\", \n \"type\": \"Patrick\",\n \"functionalLocations\": [\n {\n \"room\": {\n \"roomId\": 2,\n }\n }\n ]\n }\n}", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 710, "y": 200, "wires": [ [ "e9476b6c134f1d0f" ] ] } ]

First of all your JSON String is wrong - as the comma behind the roomID number should be removed.

As I do not like using function nodes - I put it as flow - and extracted always the last array number with the following flow:

This is extracting the last object in the array:

[
    {
        "id": "a721c86dca3c729c",
        "type": "debug",
        "z": "ebe8134f6b5af26c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 990,
        "y": 1300,
        "wires": []
    },
    {
        "id": "c0623055918b9d91",
        "type": "inject",
        "z": "ebe8134f6b5af26c",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"subject\":{\"personId\":123,\"firstName\":\"Patrick\",\"lastName\":\"2\",\"type\":\"Patrick\",\"functionalLocations\":[{\"room\":{\"roomId\":2}},{\"room\":{\"roomId\":99}}]}}",
        "payloadType": "json",
        "x": 550,
        "y": 1300,
        "wires": [
            [
                "5b060f0683ae30fb",
                "ceda27315d859fa3"
            ]
        ]
    },
    {
        "id": "5b060f0683ae30fb",
        "type": "change",
        "z": "ebe8134f6b5af26c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.subject.functionalLocations",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$reverse(payload)[0]\t",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 770,
        "y": 1300,
        "wires": [
            [
                "a721c86dca3c729c"
            ]
        ]
    },
    {
        "id": "ceda27315d859fa3",
        "type": "debug",
        "z": "ebe8134f6b5af26c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 750,
        "y": 1240,
        "wires": []
    }
]

and here the same as code for your function node:

msg.payload = 
{
    "subject": {
        "personId": 123,
        "firstName": "Patrick",
        "lastName": "2",
        "type": "Patrick",
        "functionalLocations": [
            {
                "room": {
                    "roomId": 2
                }
            },
            {
                "room": {
                    "roomId": 99
                }
            }
        ]
    }
}

msg.payload = msg.payload.subject.functionalLocations.reverse()[0];

return msg;

The comma after roomID is fine in Javascript when creating a js object, not OK when creating a JSON string.

your function could use

msg.payload = msg.payload.subject.functionalLocations[msg.payload.subject.functionalLocations.length-1]

You would then have the last array

to set the value of roomId in the last array use

msg.payload.subject.functionalLocations[msg.payload.subject.functionalLocations.length-1].room.roomId = 200;

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