Using a for loop for dynamic objects

Hello community,
I have a msg.payload with multiple objects. I want to create a for loop in a function node to be able to run through all the objects one by one and append a property that is common to all the objects. I know I have to use the property path and it looks something llike this, payload.list[object].Location.room. The problem is, there is an object in the middle of the path(object) and an id at the very end(room). I want my for loop to go look for an object, capture the id(room) and then iterate to the next object and do the same.
In the future I will have two such msg.payloads and I would want to do the same with the other msg.payload and then compare the two properties from two different msg.payloads.
Also, I am having issues defining a variable to store the required propert(room) and get it in the debug node. Any idea on how to do this?
Any help in resolving the issue would be greatly appreciated. Thank in advance.

It would be best to show us an example of exactly what the data looks like, and show us exactly what you want the result to be.

image
image
I have a list with some objects (3 objects in this case 0,1, 2) and each object has a property location which itself is another object and has a property called ward and has a subproperty wardid. I want to extract the ward ids of all the objects (0,1,2) and compare them with ward ids of another msg.payload which comes from a different node but similar data structure.

That screenshot doesn't show location as being inside the array. You can expand the objects in the array by clicking on them

You can use Array.map() to convert the array list into a new array containing just the id values. Do the same with the other one and then you can compare them.

tempsnip

Read the docs page Working with Messages, if you have not already done so, and then have a play with the map() function as I suggested. If you can't get it to do what you need then ask again and show us what you have done so far.

tempsnip
function node
Capture
Error funcloc
Okay so if I replace object (msg.call = msg.call.list[object].functionalLocation.ward.wardId) to 0, it correctly reads the wardid and gives an output 3, however when I leave it as object, it gives me an error "TypeError: Cannot read property 'functionalLocation' of undefined"
I want the object to be generalized and want the for loop to iterate after extracting wardid of each object. I also would like to append the ids into another array.

In future please post code using the method in this link. How to share code or flow json

You are overwriting msg.call the first time round the loop, which destroys what was there before, so the next time you execute that line there is no data there.
Also you should not use for in for an array you should use forEach() or a simple for loop. JavaScript Array Iteration

Do you not want to use the map method? It is much simpler.

Okay thank you, I will keep that in mind for next time.
I read about the map method and looks like the ideal solution for my case. I am still figuring out a way to implement it though.

First understand the examples in the link I posted, then you just need to provide a function that returns the wardId.

@Colin

[
    {
        "id": "c1ec9edc2c4183ce",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "539ad2d31b6e3344",
        "type": "inject",
        "z": "c1ec9edc2c4183ce",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "new_var",
                "v": "new_var",
                "vt": "jsonata"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "payload",
        "payloadType": "jsonata",
        "x": 310,
        "y": 580,
        "wires": [
            [
                "37d53d9491f339b8"
            ]
        ]
    },
    {
        "id": "37d53d9491f339b8",
        "type": "http request",
        "z": "c1ec9edc2c4183ce",
        "name": "Call GET",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "https://aqura/api/v1/nursecalls",
        "tls": "022a13a8e6daf229",
        "persist": true,
        "proxy": "",
        "authType": "bearer",
        "senderr": false,
        "credentials": {
            "password": ""
        },
        "x": 440,
        "y": 500,
        "wires": [
            [
                "c441e79c091ba8a3",
                "3d46b064c5c7f45c"
            ]
        ]
    },
    {
        "id": "c441e79c091ba8a3",
        "type": "change",
        "z": "c1ec9edc2c4183ce",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "call",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 650,
        "y": 420,
        "wires": [
            [
                "cb7417b708bdf3d8"
            ]
        ]
    },
    {
        "id": "cb7417b708bdf3d8",
        "type": "function",
        "z": "c1ec9edc2c4183ce",
        "name": "",
        "func": "var activecalls = msg.call;\nvar calls = 0;\ncalls = activecalls.map(obj => callId);\nmsg.call = calls;\nreturn msg.call;\n\n\n    \n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 880,
        "y": 340,
        "wires": [
            [
                "92e7adbf64c59867"
            ]
        ]
    },
    {
        "id": "92e7adbf64c59867",
        "type": "debug",
        "z": "c1ec9edc2c4183ce",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "call",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1100,
        "y": 340,
        "wires": []
    },
    {
        "id": "3d46b064c5c7f45c",
        "type": "debug",
        "z": "c1ec9edc2c4183ce",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 630,
        "y": 140,
        "wires": []
    },
    {
        "id": "022a13a8e6daf229",
        "type": "tls-config",
        "name": "",
        "cert": "",
        "key": "",
        "ca": "",
        "certname": "node-cert.pem",
        "keyname": "node-key.pem",
        "caname": "",
        "servername": "",
        "verifyservercert": false,
        "alpnprotocol": ""
    }
]

I tried to get one property by using map but dont think I have understood how to use it correctly. If you could help me out here it would be great.

You only needed to post the function, and tell us what is going in, which you have done already

var activecalls = msg.call;
var calls = 0;
calls = activecalls.map(obj => callId);
msg.call = calls;
return msg.call;
  1. You should return a complete msg, so at the end return msg
  2. I suggest that unless you are confident using the => operator that you use the separate function, as in the link I posted. Then the function gets passed each element of the array and just has to return the call id from the appropriate part of the element it is passed. So something like return obj.functionalLocation.ward.wardId.

@Colin Thank for your reply, this helped me solving the problem. I really appreciate the help!

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